<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 6.8, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- ra:: (version 26, updated 2023 November 21)

(c) Daniel Llorens 2005-2023

Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with no
Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. -->
<title>ra:: — An array library for C++20</title>

<meta name="description" content="ra:: — An array library for C++20">
<meta name="keywords" content="ra:: — An array library for C++20">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta name="viewport" content="width=device-width,initial-scale=1">

<link href="#Top" rel="start" title="Top">
<link href="#Indices" rel="index" title="Indices">
<link href="#Overview" rel="next" title="Overview">
<style type="text/css">
<!--
a.copiable-anchor {visibility: hidden; text-decoration: none; line-height: 0em}
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
kbd {font-style: oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
span:hover a.copiable-anchor {visibility: visible}
ul.no-bullet {list-style: none}
-->
</style>

<script type='text/javascript'>
MathJax = {
  options: {
    skipHtmlTags: {'[-]': ['pre']},
    ignoreHtmlClass: 'tex2jax_ignore',
    processHtmlClass: 'tex2jax_process'
  },
};
</script><script type="text/javascript" id="MathJax-script" async
  src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg.js">
</script>
</head>

<body lang="en">
<h1 class="settitle" align="center">ra:: — An array library for C++20</h1>



















































<div class="top" id="Top">
<div class="header">
<p>
Next: <a href="#Overview" accesskey="n" rel="next">Overview</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
</div>
<span id="ra_003a_003a"></span><h1 class="top"><code>ra::</code></h1>

<p><code>ra::</code> (version 26, updated 2023 November 21)
</p>
<p>(c) Daniel Llorens 2005&ndash;2023
</p>
<div class="display">
<pre class="display">Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with no
Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
</pre></div>

<p><code>ra::</code><a id="DOCF1" href="#FOOT1"><sup>1</sup></a> is a general purpose multidimensional array and expression template library for C++20. Please keep in mind that this manual is a work in progress. There are many errors and whole sections unwritten.
</p>



<ul class="section-toc">
<li><a href="#Overview" accesskey="1">Overview</a></li>
<li><a href="#Usage" accesskey="2">Usage</a></li>
<li><a href="#Extras" accesskey="3">Extras</a></li>
<li><a href="#Hazards" accesskey="4">Hazards</a></li>
<li><a href="#Internals" accesskey="5">Internals</a></li>
<li><a href="#The-future" accesskey="6">The future</a></li>
<li><a href="#Reference" accesskey="7">Reference</a></li>
<li><a href="#Sources" accesskey="8">Sources</a></li>
<li><a href="#Indices" accesskey="9">Indices</a></li>
<li><a href="#Notes">Notes</a></li>
</ul>
<hr>
<div class="chapter" id="Overview">
<div class="header">
<p>
Next: <a href="#Usage" accesskey="n" rel="next">Usage</a>, Previous: <a href="#Top" accesskey="p" rel="prev"><code>ra::</code></a>, Up: <a href="#Top" accesskey="u" rel="up"><code>ra::</code></a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Overview-1"></span><h2 class="chapter">1 Overview</h2>

<span id="index-length"></span>
<span id="index-rank"></span>
<span id="index-shape"></span>
<p>A multidimensional array is a container whose elements can be looked up using a multi-index (i₀, i₁, ...). Each of the indices i₀, i₁, ... has a constant range [0, n₀), [0, n₁), ... independent of the values of the other indices, so the array is ‘rectangular’. The number of indices in the multi-index is the <em>rank</em> of the array, and the list of all the <em>lengths</em> (n₀, n₁, ... nᵣ₋₁) is the <em>shape</em> of the array. We speak of a rank-<em class='tex2jax_process'>\(r\)</em> array or of an <em class='tex2jax_process'>\(r\)</em>-array.
</p>
<p>Often we deal with multidimensional <em>expressions</em> where the elements aren&rsquo;t stored anywhere, but are computed on demand when the expression is looked up. In this general sense, an ‘array’ is just a function of integers with a rectangular domain.
</p>
<p>Arrays (as a representation of <em>matrices</em>, <em>vectors</em>, or <em>tensors</em>) are common objects in math and programming, and it is very useful to be able to manipulate arrays as individual entities rather than as aggregates. Not only is
</p>
<pre class="verbatim">A = B+C;
</pre>
<p>much more compact and easier to read than
</p>
<pre class="verbatim">for (int i=0; i!=m; ++i)
    for (int j=0; j!=n; ++j)
        for (int k=0; k!=p; ++k)
            A(i, j, k) = B(i, j, k)+C(i, j, k);
</pre>
<p>but it&rsquo;s also safer and less redundant. For example, the order of the loops may be something you don&rsquo;t really care about.
</p>
<p>However, if array operations are implemented naively, a piece of code such as <code>A=B+C</code> may result in the creation of a temporary to hold <code>B+C</code> which is then assigned to <code>A</code>. This is wasteful if the arrays involved are large.
</p>
<span id="index-Blitz_002b_002b"></span>
<p>Fortunately the problem is almost as old as aggregate data types, and other programming languages have addressed it with optimizations such as <a href="https://en.wikipedia.org/wiki/Loop_fission_and_fusion">‘loop fusion’</a>, ‘drag along’ [<a href="#Sources">Abr70</a>]
, or ‘deforestation’ [<a href="#Sources">Wad90</a>]
. In the C++ context the technique of ‘expression templates’ was pioneered in the late 90s by libraries such as Blitz++ [<a href="#Sources">bli17</a>]
. It works by making <code>B+C</code> into an ‘expression object’ which holds references to its arguments and performs the sum only when its elements are looked up. The compiler removes the temporary expression objects during optimization, so that <code>A=B+C</code> results (in principle) in the same generated code as the complicated loop nest above.
</p>

<ul class="section-toc">
<li><a href="#Rank-polymorphism" accesskey="1">Rank polymorphism</a></li>
<li><a href="#Drag-along-and-beating" accesskey="2">Drag along and beating</a></li>
<li><a href="#Why-C_002b_002b" accesskey="3">Why C++</a></li>
<li><a href="#Guidelines" accesskey="4">Guidelines</a></li>
<li><a href="#Other-libraries" accesskey="5">Other array libraries</a></li>
</ul>
<hr>
<div class="section" id="Rank-polymorphism">
<div class="header">
<p>
Next: <a href="#Drag-along-and-beating" accesskey="n" rel="next">Drag along and beating</a>, Up: <a href="#Overview" accesskey="u" rel="up">Overview</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Rank-polymorphism-1"></span><h3 class="section">1.1 Rank polymorphism</h3>

<p><em>Rank polymorphism</em> is the ability to treat an array of rank <em class='tex2jax_process'>\(r\)</em> as an array of lower rank where the elements are themselves arrays.
</p>
<span id="index-cell"></span>
<span id="index-frame"></span>
<p>For example, think of a matrix A, a 2-array with shape (n₀, n₁) where the elements A(i₀, i₁) are numbers. If we consider the subarrays A(0, ...), A(1, ...), ..., A(n₀-1, ...) as individual elements, then we have a new view of A as a 1-array of length n₀ with those rows as elements. We say that the rows A(i₀)≡A(i₀, ...) are the 1-<em>cells</em> of A, and the numbers A(i₀, i₁) are 0-cells of A. For an array of arbitrary rank <em class='tex2jax_process'>\(r\)</em> the (<em class='tex2jax_process'>\(r\)</em>-1)-cells of A are called its <em>items</em>. The prefix of the shape (n₀, n₁, ... nₙ₋₁₋ₖ) that is not taken up by the k-cell is called the k-<em>frame</em>.
</p>
<p>An obvious way to store an array in linearly addressed memory is to place its items one after another. So we would store a 3-array as
</p>
<blockquote>
<p>A: [A(0), A(1), ...]
</p></blockquote>

<p>and the items of A(i₀), etc. are in turn stored in the same way, so
</p>
<blockquote>
<p>A: [A(0): [A(0, 0), A(0, 1) ...], ...]
</p></blockquote>

<p>and the same for the items of A(i₀, i₁), etc.
</p>
<blockquote>
<p>A: [[A(0, 0): [A(0, 0, 0), A(0, 0, 1) ...], A(0, 1): [A(0, 1, 0), A(0, 1, 1) ...]], ...]
</p></blockquote>

<span id="index-order_002c-row_002dmajor"></span>
<p>This way to lay out an array in memory is called <em>row-major order</em> or <em>C-order</em>, since it&rsquo;s the default order for built-in arrays in C (see <a href="#Other-libraries">Other array libraries</a>). A row-major array A with shape (n₀, n₁, ... nᵣ₋₁) can be looked up like this:
</p>
<span id="x_002dsteps"></span><blockquote>
<p>A(i₀, i₁, ...) = (storage-of-A) [(((i₀n₁ + i₁)n₂ + i₂)n₃ + ...)+iᵣ₋₁] = (storage-of-A) [o + s₀i₀ + s₁i₁ +  ...]
</p></blockquote>

<span id="index-step"></span>
<span id="index-stride"></span>
<p>where the numbers (s₀, s₁, ...) are called the <em>steps</em><a id="DOCF2" href="#FOOT2"><sup>2</sup></a>. Note that the ‘linear’ or ‘raveled’ address [o + s₀i₀ + s₁i₁ +  ...] is an affine function of (i₀, i₁, ...). If we represent an array as a tuple
</p>
<blockquote>
<p>A ≡ ((storage-of-A), o, (s₀, s₁, ...))
</p></blockquote>

<p>then any affine transformation of the indices can be achieved simply by modifying the numbers (o, (s₀, s₁, ...)), with no need to touch the storage. This includes common operations such as: <a href="#x_002dtranspose">transposing</a> axes, <a href="#x_002dreverse">reversing</a> the order along an axis, most cases of <a href="#Slicing">slicing</a>, and sometimes even reshaping or tiling the array.
</p>
<p>A basic example is obtaining the i₀-th item of A:
</p>
<blockquote>
<p>A(i₀) ≡ ((storage-of-A), o+s₀i₀, (s₁, ...))
</p></blockquote>

<p>Note that we can iterate over these items by simply bumping the pointer o+s₀i₀. This means that iterating over (k&gt;0)-cells doesn&rsquo;t cost any  more than iterating over 0-cells (see <a href="#Cell-iteration">Cell iteration</a>).
</p>
<hr>
</div>
<div class="section" id="Drag-along-and-beating">
<div class="header">
<p>
Next: <a href="#Why-C_002b_002b" accesskey="n" rel="next">Why C++</a>, Previous: <a href="#Rank-polymorphism" accesskey="p" rel="prev">Rank polymorphism</a>, Up: <a href="#Overview" accesskey="u" rel="up">Overview</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Drag-along-and-beating-1"></span><h3 class="section">1.2 Drag along and beating</h3>

<p>These two fundamental array optimizations are described in [<a href="#Sources">Abr70</a>]
.
</p>
<p><em>Drag-along</em> is the process that delays evaluation of array operations. Expression templates can be seen as an implementation of drag-along. Drag-along isn&rsquo;t an optimization in and of itself; it simply preserves the necessary information up to the point where the expression can be executed efficiently.
</p>
<p><em>Beating</em> is the implementation of certain array operations on the array <a href="#Containers-and-views">view</a> descriptor instead of on the array contents. For example, if <code>A</code> is a 1-array, one can implement <a href="#x_002dreverse"><code>reverse(A, 0)</code></a> by negating the <a href="#x_002dsteps">step</a> and moving the offset to the other end of the array, without having to move any elements. More generally, beating applies to any function-of-indices (generator) that can take the place of an array in an array expression. For instance, an expression such as <a href="#x_002diota"><code>1+iota(3, 0)</code></a> can be beaten into <code>iota(3, 1)</code>, and this can enable further optimizations.
</p>
<hr>
</div>
<div class="section" id="Why-C_002b_002b">
<div class="header">
<p>
Next: <a href="#Guidelines" accesskey="n" rel="next">Guidelines</a>, Previous: <a href="#Drag-along-and-beating" accesskey="p" rel="prev">Drag along and beating</a>, Up: <a href="#Overview" accesskey="u" rel="up">Overview</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Why-C_002b_002b-1"></span><h3 class="section">1.3 Why C++</h3>

<p>Of course the main reason is that (this being a personal project) I&rsquo;m more familiar with C++ than with other languages to which the following might apply.
</p>
<p>C++ supports the low level control that is necessary for interoperation with external libraries and languages, but still has the abstraction power to create the features we want even though the language has no native support for most of them.
</p>
<span id="index-APL"></span>
<span id="index-J"></span>
<p>The classic array languages, APL [<a href="#Sources">FI73</a>]
 and J [<a href="#Sources">Ric08</a>]
, have array support baked in. The same is true for other languages with array facilities such as Fortran or Octave/Matlab. Array libraries for general purpose languages usually depend heavily on C extensions. In Numpy&rsquo;s case [<a href="#Sources">num17</a>]
 this is both for reasons of flexibility (e.g. to obtain predictable memory layout and machine types) and of performance.
</p>
<p>On the other extreme, an array library for C would be hampered by the limited means of abstraction in the language (no polymorphism, no metaprogramming, etc.) so the natural choice of C programmers is to resort to code generators, which eventually turn into new languages.
</p>
<p>In C++, a library is enough.
</p>
<hr>
</div>
<div class="section" id="Guidelines">
<div class="header">
<p>
Next: <a href="#Other-libraries" accesskey="n" rel="next">Other array libraries</a>, Previous: <a href="#Why-C_002b_002b" accesskey="p" rel="prev">Why C++</a>, Up: <a href="#Overview" accesskey="u" rel="up">Overview</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Guidelines-1"></span><h3 class="section">1.4 Guidelines</h3>

<p><code>ra::</code> attempts to be general, consistent, and transparent.
</p>
<p>Generality is achieved by removing arbitrary restrictions and by adopting the rank extension mechanism of J. <code>ra::</code> supports array operations with an arbitrary number of arguments. Any of the arguments in an array expression can be read from or written to. Arrays or array expressions can be of any rank. Slicing operations work for subscripts of any rank, as in APL. You can use your own types as array elements.
</p>
<p>Consistency is achieved by having a clear set of concepts and having the realizations of those concepts adhere to the concept as closely as possible. <code>ra::</code> offers a few different types of views and containers, but it should be possible to use them interchangeably whenever it makes sense. For example, it used to be the case that you couldn&rsquo;t create a higher rank iterator on a <code>SmallView</code>, even though you could do it on a <code>View</code>; this was a bug.
</p>
<p>Sometimes consistency requires a choice. For example, given array views A and B, <code>A=B</code> copies the contents of view <code>B</code> into view <code>A</code>. To change view <code>A</code> instead (to treat <code>A</code> as a pointer) would be the default meaning of <code>A=B</code> for C++ types, and result in better consistency with the rest of the language, but I have decided that having consistency between views and containers (which ‘are’ their contents in a sense that views aren&rsquo;t) is more important.
</p>
<p>Transparency is achieved by avoiding unnecessary abstraction. An array view consists of a pointer and a list of steps and I see no point in hiding it. Manipulating the steps directly is often useful. A container consists of storage and a view and that isn&rsquo;t hidden either. Some of the types have an obscure implementation but I consider that a defect. Ideally you should be able to rewrite expressions on the fly, or plug in your own traversal methods or storage handling.
</p>
<p>That isn&rsquo;t to mean that you need to be in command of a lot of internal detail to be able to use the library. I hope to have provided a high level interface to most operations and a reasonably usable syntax. However, transparency is critical to achieve interoperation with external libraries and languages. When you need to, you&rsquo;ll be able to guarantee that an array is stored in compact columns, or that the real parts are interleaved with the imaginary parts.
</p>
<hr>
</div>
<div class="section" id="Other-libraries">
<div class="header">
<p>
Previous: <a href="#Guidelines" accesskey="p" rel="prev">Guidelines</a>, Up: <a href="#Overview" accesskey="u" rel="up">Overview</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Other-array-libraries"></span><h3 class="section">1.5 Other array libraries</h3>

<p>Here I try to list the C++ array libraries that I know of, or libraries that I think deserve a mention for the way they deal with arrays. It is not an extensive review, since I have only used a few of these libraries myself. Please follow the links if you want to be properly informed.
</p>
<p>Since the C++ standard library doesn&rsquo;t offer a standard multidimensional array type, some libraries for specific tasks (linear algebra operations, finite elements, optimization) offer an accessory array library, which may be more or less general. Other libraries have generic array interfaces without needing to provide an array type. FFTW is a good example, maybe because it isn&rsquo;t C++!
</p>
<ul class="section-toc">
<li><a href="#Standard-C_002b_002b" accesskey="1">Standard C++</a></li>
<li><a href="#Blitz_002b_002b" accesskey="2">Blitz++</a></li>
<li><a href="#Other-C_002b_002b-libraries" accesskey="3">Other C++ libraries</a></li>
<li><a href="#Other-languages" accesskey="4">Other languages</a></li>
</ul>
<div class="subsection" id="Standard-C_002b_002b">
<h4 class="subsection">1.5.1 Standard C++</h4>

<p>The C++ language offers multidimensional arrays as a legacy feature from C, e.g. <code>int a[3][4]</code>. These decay to pointers when you do nearly anything with them, don&rsquo;t know their own shape or rank at runtime, and are generally too limited.
</p>
<p>The C++ standard library also offers a number of contiguous storage containers that can be used as 1-arrays: <code>&lt;array&gt;</code>, <code>&lt;vector&gt;</code> and <code>&lt;valarray&gt;</code>. Neither supports higher ranks out of the box, but <code>&lt;valarray&gt;</code> offers array operations for 1-arrays. <code>ra::</code> makes use of <code>&lt;array&gt;</code> and <code>&lt;vector&gt;</code> for storage and bootstrapping.
</p>
<p><code>ra::</code> accepts built-in arrays and standard library types as array objects (see <a href="#Compatibility">Compatibility</a>).
</p>
</div>
<div class="subsection" id="Blitz_002b_002b">
<h4 class="subsection">1.5.2 Blitz++</h4>
<span id="index-Blitz_002b_002b-1"></span>
<p>Blitz++ [<a href="#Sources">bli17</a>]
 pioneered the use of expression templates in C++. It supported higher rank arrays, as high as it was practical in C++98, but not runtime rank. It also supported small arrays with compile time shape (<code>Tiny</code>), and convenience features such as Fortran-order constructors and arbitrary lower bounds for the array indices (both of which <code>ra::</code> chooses not to support). It placed a strong emphasis on performance, with array traversal methods such as blocking, space filling curves, etc.
</p>
<p>However, the implementation had to fight the limitations of C++98, and it offered no general rank extension mechanism.
</p>
<p>One important difference between Blitz++ and <code>ra::</code> is that Blitz++&rsquo;s arrays were reference counted. <code>ra::</code> doesn&rsquo;t do any memory management on its own: the default container (data-owning) types are values, and views are distinct types. You can select your own storage for the data-owning objects, including reference-counted storage (<code>ra::</code> declares a type using <code>std::shared_ptr</code>), but this is not the default.
</p>
</div>
<div class="subsection" id="Other-C_002b_002b-libraries">
<h4 class="subsection">1.5.3 Other C++ libraries</h4>

<p>TODO
</p>
</div>
<div class="subsection" id="Other-languages">
<h4 class="subsection">1.5.4 Other languages</h4>

<p>TODO Maybe review other languages, at least the big ones (Fortran / APL / J / Matlab / Python-Numpy).
</p>
<hr>
</div>
</div>
</div>
<div class="chapter" id="Usage">
<div class="header">
<p>
Next: <a href="#Extras" accesskey="n" rel="next">Extras</a>, Previous: <a href="#Overview" accesskey="p" rel="prev">Overview</a>, Up: <a href="#Top" accesskey="u" rel="up"><code>ra::</code></a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Usage-1"></span><h2 class="chapter">2 Usage</h2>

<p>This is an extended exposition of the features of <code>ra::</code> and is probably best read in order. For details on specific functions or types, please see <a href="#Reference">Reference</a>.
</p>

<ul class="section-toc">
<li><a href="#Using-the-library" accesskey="1">Using <code>ra::</code></a></li>
<li><a href="#Containers-and-views" accesskey="2">Containers and views</a></li>
<li><a href="#Array-operations" accesskey="3">Array operations</a></li>
<li><a href="#Rank-extension" accesskey="4">Rank extension</a></li>
<li><a href="#Cell-iteration" accesskey="5">Cell iteration</a></li>
<li><a href="#Slicing" accesskey="6">Slicing</a></li>
<li><a href="#Functions" accesskey="7">Functions</a></li>
<li><a href="#The-rank-conjunction" accesskey="8">The rank conjunction</a></li>
<li><a href="#Compatibility" accesskey="9">Compatibility</a></li>
<li><a href="#Extension">Extension</a></li>
<li><a href="#Error-handling">Error handling</a></li>
</ul>
<hr>
<div class="section" id="Using-the-library">
<div class="header">
<p>
Next: <a href="#Containers-and-views" accesskey="n" rel="next">Containers and views</a>, Up: <a href="#Usage" accesskey="u" rel="up">Usage</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Using-ra_003a_003a"></span><h3 class="section">2.1 Using <code>ra::</code></h3>

<p><code>ra::</code> is a header only library with no dependencies other than the standard library, so you just need to place the &lsquo;<samp>ra/</samp>&rsquo; folder somewhere in your include path and add <code>#include &quot;ra/ra.hh&quot;</code> at the top of your sources.
</p>
<p>A compiler with C++20 support is required. For the current version this means at least <b>gcc 11</b> with <samp>-std=c++20</samp>. Some C++23 features are available with <samp>-std=c++2b</samp>. Check the top <code>README.md</code> for more up-to-date information.
</p>
<p>Here is a minimal program<a id="DOCF3" href="#FOOT3"><sup>3</sup></a>:
</p>
<div class="example">
<pre class="verbatim">#include &quot;ra/ra.hh&quot;
#include &lt;iostream&gt;

int main()
{
    ra::Big&lt;char, 2&gt; A({2, 5}, &quot;helloworld&quot;);
    std::cout &lt;&lt; ra::noshape &lt;&lt; format_array(transpose&lt;1, 0&gt;(A), &quot;|&quot;) &lt;&lt; std::endl;
}
</pre><pre class="example">-| h|w
   e|o
   l|r
   l|l
   d|d
</pre></div>

<p>The following headers are <em>not</em> included by default:
</p><ul>
<li> <code>&quot;ra/dual.hh&quot;</code>: A dual number type for simple uses of automatic differentiation.

</li><li> <code>&quot;ra/test.hh&quot;</code>, <code>&quot;ra/bench.hh&quot;</code>: Used by the test and benchmark suites.
</li></ul>

<p>The header <code>&quot;ra/bootstrap.hh&quot;</code> can be used to configure <a href="#Error-handling">Error handling</a>. You don&rsquo;t need to modify the header, but the configuration depends on including <code>&quot;ra/bootstrap.hh&quot;</code> before the rest of <code>ra::</code> in order to override the default handler. All other headers are for internal use by <code>ra::</code>.
</p>
<span id="index-container"></span>
<hr>
</div>
<div class="section" id="Containers-and-views">
<div class="header">
<p>
Next: <a href="#Array-operations" accesskey="n" rel="next">Array operations</a>, Previous: <a href="#Using-the-library" accesskey="p" rel="prev">Using <code>ra::</code></a>, Up: <a href="#Usage" accesskey="u" rel="up">Usage</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Containers-and-views-1"></span><h3 class="section">2.2 Containers and views</h3>

<p><code>ra::</code> offers two kinds of data objects. The first kind, the <em>container</em>, owns its data. Creating a container uses up memory and destroying it causes that memory to be freed.
</p>
<span id="index-compile_002dtime"></span>
<span id="index-ct"></span>
<span id="index-runtime"></span>
<span id="index-rt"></span>
<p>There are three kinds of containers (ct: compile-time, rt: runtime): 1) ct size, 2) ct rank/rt shape, and 3) rt rank; rt rank implies rt shape. Some rt size arrays can be resized but rt rank arrays cannot normally have their rank changed. Instead, you create a new container or view with the rank you want.
</p>
<p>For example:
</p>
<div class="example">
<pre class="verbatim">{
    ra::Small&lt;double, 2, 3&gt; a(0.);     // a ct size 2x3 array
    ra::Big&lt;double, 2&gt; b({2, 3}, 0.);  // a rt size 2x3 array
    ra::Big&lt;double&gt; c({2, 3}, 0.);     // a rt rank 2x3 array
    // a, b, c destroyed at end of scope
}
</pre></div>

<p>Using the right kind of container can result in better performance. Ct shapes do not need to be stored in memory, which matters when you have many small arrays. Ct shape or ct rank arrays are also safer to use; sometimes <code>ra::</code> will be able to detect errors in the shapes or ranks of array operands at compile time, if the appropriate types are used.
</p>
<p>Container constructors come in two forms. The first form takes a single argument which is copied into the new container. This argument provides shape information if the container type requires it.<a id="DOCF4" href="#FOOT4"><sup>4</sup></a>
</p>
<div class="example">
<pre class="verbatim">using ra::Small, ra::Big;
Small&lt;int, 2, 2&gt; a = {{1, 2}, {3, 4}};  // explicit contents
Big&lt;int, 2&gt; a1 = {{1, 2}, {3, 4}};      // explicit contents
Small&lt;int, 2, 2&gt; a2 = {{1, 2}};         // error: bad shape
Small&lt;int, 2, 2&gt; b = 7;                 // 7 is copied into b
Small&lt;int, 2, 2&gt; c = a;                 // the contents of a are copied into c
Big&lt;int&gt; d = a;                         // d takes the shape of a and a is copied into d
Big&lt;int&gt; e = 0;                         // e is a 0-array with one element f()==0.
</pre></div>

<p>The second form takes two arguments, one giving the shape, the second the contents.
</p>
<span id="index-none"></span>
<span id="index-uninitialized-container"></span>
<div class="example">
<pre class="verbatim">ra::Big&lt;double, 2&gt; a({2, 3}, 1.);     // a has shape [2 3], filled with 1.
ra::Big&lt;double&gt; b({2, 3}, ra::none);  // b has shape [2 3], default initialized
ra::Big&lt;double&gt; c({2, 3}, a);         // c has shape [2 3], a is copied into c
</pre></div>

<p>The last example may result in an error if the shape of <code>a</code> and (2,&nbsp;<!-- /@w -->3) don&rsquo;t match. Here the shape of <code>1.</code> [which is ()] matches (2,&nbsp;<!-- /@w -->3) by a mechanism of rank extension (see <a href="#Rank-extension">Rank extension</a>). The special value <code>ra::none</code> can be used to request <a href="https://en.cppreference.com/w/cpp/language/default_initialization">default initialization</a> of the container&rsquo;s elements.
</p>
<p>The shape argument can have rank 0 only for rank 1 arrays.
</p>
<span id="index-none-1"></span>
<span id="index-uninitialized-container-1"></span>
<div class="example">
<pre class="verbatim">ra::Big&lt;int&gt; c(3, 0);      // ok {0, 0, 0}, same as ra::Big&lt;int&gt; c({3}, 0)
ra::Big&lt;int, 1&gt; c(3, 0);   // ok {0, 0, 0}, same as ra::Big&lt;int, 1&gt; c({3}, 0)
ra::Big&lt;int, 2&gt; c({3}, 0); // error: bad length for shape
ra::Big&lt;int, 2&gt; c(3, 0);   // error: bad length for shape
</pre></div>

<p>When the content argument is a pointer or a 1D brace list, it&rsquo;s handled especially, not for shape<a id="DOCF5" href="#FOOT5"><sup>5</sup></a>, but only as the (row-major) ravel of the content. The pointer constructor is unsafe —use at your own risk!<a id="DOCF6" href="#FOOT6"><sup>6</sup></a>
</p>
<span id="index-order_002c-column_002dmajor"></span>
<div class="example">
<pre class="verbatim">Small&lt;int, 2, 2&gt; aa = {1, 2, 3, 4}; // ravel of the content

ra::Big&lt;double, 2&gt; a({2, 3}, {1, 2, 3, 4, 5, 6}); // same as a = {{1, 2, 3}, {4, 5, 6}}
</pre></div>

<div class="example">
<pre class="verbatim">double bx[6] = {1, 2, 3, 4, 5, 6}
ra::Big&lt;double, 2&gt; b({3, 2}, bx); // {{1, 2}, {3, 4}, {5, 6}}

double cx[4] = {1, 2, 3, 4}
ra::Big&lt;double, 2&gt; c({3, 2}, cx); // *** WHO NOSE ***
</pre></div>

<div class="example">
<pre class="verbatim">using lens = mp::int_list&lt;2, 3&gt;;
using steps = mp::int_list&lt;1, 2&gt;;
ra::SmallArray&lt;double, lens, steps&gt; a {{1, 2, 3}, {4, 5, 6}}; // stored column-major: 1 4 2 5 3 6
</pre></div>

<p>These produce compile time errors:
</p>
<div class="example">
<pre class="verbatim">Big&lt;int, 2&gt; b = {1, 2, 3, 4};           // error: shape cannot be deduced from ravel
Small&lt;int, 2, 2&gt; b = {1, 2, 3, 4 5};    // error: bad size
Small&lt;int, 2, 2&gt; b = {1, 2, 3};         // error: bad size
</pre></div>

<span id="x_002dscalar_002dchar_002dstar"></span><p>Sometimes the pointer constructor gets in the way (see <a href="#x_002dscalar"><code>scalar</code></a>): </p>
<div class="example">
<pre class="verbatim">ra::Big&lt;char const *, 1&gt; A({3}, &quot;hello&quot;); // error: try to convert char to char const *
ra::Big&lt;char const *, 1&gt; A({3}, ra::scalar(&quot;hello&quot;)); // ok, &quot;hello&quot; is a single item
cout &lt;&lt; ra::noshape &lt;&lt; format_array(A, &quot;|&quot;) &lt;&lt; endl;
</pre><pre class="example">-| hello|hello|hello
</pre></div>

<span id="index-view"></span>
<p>A <em>view</em> is similar to a container in that it points to actual data in memory. However, the view doesn&rsquo;t own that data and destroying the view won&rsquo;t affect it. For example:
</p>
<div class="example">
<pre class="verbatim">ra::Big&lt;double&gt; c({2, 3}, 0.);     // a rt rank 2x3 array
{
    auto c1 = c(1);                // the second row of array c
    // c1 is destroyed here
}
cout &lt;&lt; c(1, 1) &lt;&lt; endl;           // ok
</pre></div>

<p>The data accessed through a view is the data of the ‘root’ container, so modifying the former will be reflected in the latter.
</p>
<div class="example">
<pre class="verbatim">ra::Big&lt;double&gt; c({2, 3}, 0.);
auto c1 = c(1);
c1(2) = 9.;                        // c(1, 2) = 9.
</pre></div>

<p>Just as for containers, there are separate types of views depending on whether the shape is known at compile time, the rank is known at compile time but the shape is not, or neither the shape nor the rank are known at compile time. <code>ra::</code> has functions to create the most common kinds of views:
</p>
<div class="example">
<pre class="verbatim">ra::Big&lt;double&gt; c {{1, 2, 3}, {4, 5, 6}};
auto ct = transpose&lt;1, 0&gt;(c); // {{1, 4}, {2, 5}, {3, 6}}
auto cr = reverse(c, 0); // {{4, 5, 6}, {1, 2, 3}}
</pre></div>

<p>However, views can point to anywhere in memory and that memory doesn&rsquo;t have to belong to an <code>ra::</code> container. For example:
</p>
<div class="example">
<pre class="verbatim">int raw[6] = {1, 2, 3, 4, 5, 6};
ra::View&lt;int&gt; v1({{2, 3}, {3, 1}}, raw); // view with shape [2, 3] steps [3, 1]
ra::View&lt;int&gt; v2({2, 3}, raw);           // same, default C (row-major) steps
</pre></div>

<p>Containers can be treated as views of the same kind (rt or ct) . If you declare a function
</p>
<div class="example">
<pre class="verbatim">void f(ra::View&lt;int, 3&gt; &amp; v);
</pre></div>

<p>you may pass it an object of type <code>ra::Big&lt;int, 3&gt;</code>.
</p>

<hr>
</div>
<div class="section" id="Array-operations">
<div class="header">
<p>
Next: <a href="#Rank-extension" accesskey="n" rel="next">Rank extension</a>, Previous: <a href="#Containers-and-views" accesskey="p" rel="prev">Containers and views</a>, Up: <a href="#Usage" accesskey="u" rel="up">Usage</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Array-operations-1"></span><h3 class="section">2.3 Array operations</h3>

<p>To apply an operation to each element of an array, use the function <a href="#x_002dfor_005feach"><code>for_each</code></a>. The array is traversed in an order that is decided by the library.
</p>
<div class="example">
<pre class="verbatim">ra::Small&lt;double, 2, 3&gt; a = {{1, 2, 3}, {4, 5, 6}};
double s = 0.;
for_each([&amp;s](auto &amp;&amp; a) { s+=a; }, a);
</pre><pre class="example">&rArr; s = 21.
</pre></div>

<p>To construct an array expression but stop short of traversing it, use the function <code>map</code>. The expression will be traversed when it is assigned to a view, printed out, etc.
</p>
<div class="example">
<pre class="verbatim">using T = ra::Small&lt;double, 2, 2&gt;;
T a = {{1, 2}, {3, 4}};
T b = {{10, 20}, {30, 40}};
T c = map([](auto &amp;&amp; a, auto &amp;&amp; b) { return a+b; }, a, b); // (1)
</pre><pre class="example">&rArr; c = {{11, 22}, {33, 44}}
</pre></div>

<p>Expressions may take any number of arguments and be nested arbitrarily.
</p>
<div class="example">
<pre class="verbatim">T d = 0;
for_each([](auto &amp;&amp; a, auto &amp;&amp; b, auto &amp;&amp; d) { d = a+b; },
         a, b, d); // same as (1)
for_each([](auto &amp;&amp; ab, auto &amp;&amp; d) { d = ab; },
         map([](auto &amp;&amp; a, auto &amp;&amp; b) { return a+b; },
             a, b),
         d); // same as (1)
</pre></div>

<p>The operator of an expression may return a reference and you may assign to an expression in that case. <code>ra::</code> will complain if the expression is somehow not assignable.
</p>
<div class="example">
<pre class="verbatim">T d = 0;
map([](auto &amp; d) -&gt; decltype(auto) { return d; }, d) // just pass d along
  = map([](auto &amp;&amp; a, auto &amp;&amp; b) { return a+b; }, a, b); // same as (1)
</pre></div>

<p><code>ra::</code> defines many shortcuts for common array operations. You can of course just do:
</p>
<div class="example">
<pre class="verbatim">T c = a+b; // same as (1)
</pre></div>

<hr>
</div>
<div class="section" id="Rank-extension">
<div class="header">
<p>
Next: <a href="#Cell-iteration" accesskey="n" rel="next">Cell iteration</a>, Previous: <a href="#Array-operations" accesskey="p" rel="prev">Array operations</a>, Up: <a href="#Usage" accesskey="u" rel="up">Usage</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Rank-extension-1"></span><h3 class="section">2.4 Rank extension</h3>

<p>Rank extension is the mechanism that allows <code>R+S</code> to be defined even when <code>R</code>, <code>S</code> may have different ranks. The idea is an interpolation of the following basic cases.
</p>
<p>Suppose first that <code>R</code> and <code>S</code> have the same rank. We require that the shapes be the same. Then the shape of <code>R+S</code> will be the same as the shape of either <code>R</code> or <code>S</code> and the elements of <code>R+S</code> will be
</p>
<blockquote>
<p><code>(R+S)(i₀ i₁ ... i₍ᵣ₋₁₎) = R(i₀ i₁ ... i₍ᵣ₋₁₎) + S(i₀ i₁ ... i₍ᵣ₋₁₎)</code>
</p></blockquote>

<p>where <code>r</code> is the rank of <code>R</code>.
</p>
<p>Now suppose that <code>S</code> has rank 0. The shape of <code>R+S</code> is the same as the shape of <code>R</code> and the elements of <code>R+S</code> will be
</p>
<blockquote>
<p><code>(R+S)(i₀ i₁ ... i₍ᵣ₋₁₎) = R(i₀ i₁ ... i₍ᵣ₋₁₎) + S()</code>.
</p></blockquote>

<p>The two rules above are supported by all primitive array languages, e.g. Matlab [<a href="#Sources">Mat</a>]
. But suppose that <code>S</code> has rank <code>s</code>, where <code>0&lt;s&lt;r</code>. Looking at the expressions above, it seems natural to define <code>R+S</code> by
</p>
<blockquote>
<p><code>(R+S)(i₀ i₁ ... i₍ₛ₋₁₎ ... i₍ᵣ₋₁₎) = R(i₀ i₁ ... i₍ₛ₋₁₎ ... i₍ᵣ₋₁₎) + S(i₀ i₁ ... i₍ₛ₋₁₎)</code>.
</p></blockquote>

<p>That is, after we run out of indices in <code>S</code>, we simply repeat the elements. We have aligned the shapes so:
</p>
<blockquote>
<pre class="verbatim">[n₀ n₁ ... n₍ₛ₋₁₎ ... n₍ᵣ₋₁₎]
[n₀ n₁ ... n₍ₛ₋₁₎]
</pre></blockquote>

<span id="index-shape-agreement_002c-prefix"></span>
<span id="index-shape-agreement_002c-suffix"></span>
<span id="index-Numpy"></span>
<p>This rank extension rule is used by the J language [<a href="#Sources">J S</a>]
 and is known as <em>prefix agreement</em>. The opposite rule of <em>suffix agreement</em> is used, for example, in Numpy [<a href="#Sources">num17</a>]
<a id="DOCF7" href="#FOOT7"><sup>7</sup></a>.
</p>
<p>As you can verify, the prefix agreement rule is distributive. Therefore it can be applied to nested expressions or to expressions with any number of arguments. It is applied systematically throughout <code>ra::</code>, even in assignments. For example,
</p>
<div class="example">
<pre class="verbatim">ra::Small&lt;int, 3&gt; x {3, 5, 9};
ra::Small&lt;int, 3, 2&gt; a = x; // assign x(i) to each a(i, j)
</pre><pre class="example">&rArr; a = {{3, 3}, {5, 5}, {9, 9}}
</pre></div>

<div class="example">
<pre class="verbatim">ra::Small&lt;int, 3&gt; x(0.);
ra::Small&lt;int, 3, 2&gt; a = {{1, 2}, {3, 4}, {5, 6}};
x += a; // sum the rows of a
</pre><pre class="example">&rArr; x = {3, 7, 11}
</pre></div>

<div class="example">
<pre class="verbatim">ra::Big&lt;double, 3&gt; a({5, 3, 3}, ra::_0);
ra::Big&lt;double, 1&gt; b({5}, 0.);
b += transpose&lt;0, 1, 1&gt;(a); // b(i) = ∑ⱼ a(i, j, j)
</pre><pre class="example">&rArr; b = {0, 3, 6, 9, 12}
</pre></div>

<span id="index-Numpy-1"></span>
<span id="index-broadcasting_002c-singleton_002c-newaxis"></span>
<p>An weakness of prefix agreement is that the axes you want to match aren&rsquo;t always the prefix axes. Other array systems offer a feature similar to rank extension called ‘broadcasting’ that is a bit more flexible. For example, in the way it&rsquo;s implemented in Numpy [<a href="#Sources">num17</a>]
, an array of shape [A B 1 D] will match an array of shape [A B C D]. The process of broadcasting consists in inserting so-called ‘singleton dimensions’ (axes with length one) to align the axes that one wishes to match. You can think of prefix agreement as a particular case of broadcasting where the singleton dimensions are added to the end of the shorter shapes automatically.
</p>
<p>A drawback of singleton broadcasting is that it muddles the distinction between a scalar and a vector of length 1. Sometimes, an axis of length 1 is no more than that, and if 2≠3 is a size mismatch, it isn&rsquo;t obvious why 1≠2 shouldn&rsquo;t be. To avoid this problem, <code>ra::</code> supports broadcasting with undefined length axes (see <a href="#x_002dinsert"><code>insert</code></a>).
</p>
<div class="example">
<pre class="verbatim">ra::Big&lt;double, 3&gt; a({5, 3}, ra::_0);
ra::Big&lt;double, 1&gt; b({3}, 0.);
ra::Big&lt;double, 3&gt; c({1, 3}, ra::_0);

// b(?, i) += a(j, i) → b(i) = ∑ⱼ a(j, i) (sum columns)
b(ra::insert&lt;1&gt;) += a;

c = a; // 1 ≠ 5, still an agreement error
</pre></div>

<p>Still another way to align array axes is provided by the <a href="#The-rank-conjunction">rank conjunction</a>.
</p>
<p>Even with axis insertion, it is still necessary that the axes one wishes to match are in the same order in all the arguments.
<a href="#x_002dtranspose">Transposing</a> the axes before extension is a possible workaround.
</p>
<hr>
</div>
<div class="section" id="Cell-iteration">
<div class="header">
<p>
Next: <a href="#Slicing" accesskey="n" rel="next">Slicing</a>, Previous: <a href="#Rank-extension" accesskey="p" rel="prev">Rank extension</a>, Up: <a href="#Usage" accesskey="u" rel="up">Usage</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Cell-iteration-1"></span><h3 class="section">2.5 Cell iteration</h3>

<p><code>map</code> and <code>for_each</code> apply their operators to each element of their arguments; in other words, to the 0-cells of the arguments. But it is possible to specify directly the rank of the cells that one iterates over:
</p>
<div class="example">
<pre class="verbatim">ra::Big&lt;double, 3&gt; a({5, 4, 3}, ra::_0);
for_each([](auto &amp;&amp; b) { /* b has shape (5 4 3) */ }, iter&lt;3&gt;(a));
for_each([](auto &amp;&amp; b) { /* b has shape (4 3) */ }, iter&lt;2&gt;(a));
for_each([](auto &amp;&amp; b) { /* b has shape (3) */ }, iter&lt;1&gt;(a));
for_each([](auto &amp;&amp; b) { /* b has shape () */ }, iter&lt;0&gt;(a)); // elements
for_each([](auto &amp;&amp; b) { /* b has shape () */ }, a); // same as iter&lt;0&gt;(a); default
</pre></div>

<p>One may specify the <em>frame</em> rank instead:
</p>
<div class="example">
<pre class="verbatim">for_each([](auto &amp;&amp; b) { /* b has shape () */ }, iter&lt;-3&gt;(a)); // same as iter&lt;0&gt;(a)
for_each([](auto &amp;&amp; b) { /* b has shape (3) */ }, iter&lt;-2&gt;(a)); // same as iter&lt;1&gt;(a)
for_each([](auto &amp;&amp; b) { /* b has shape (4 3) */ }, iter&lt;-1&gt;(a)); // same as iter&lt;2&gt;(a)
</pre></div>

<p>In this way it is possible to match shapes in various ways. Compare
</p>
<div class="example">
<pre class="verbatim">ra::Big&lt;double, 2&gt; a = {{1, 2, 3}, {4, 5, 6}};
ra::Big&lt;double, 1&gt; b = {10, 20};
ra::Big&lt;double, 2&gt; c = a * b; // multiply (each item of a) by (each item of b)
</pre><pre class="example">&rArr; a = {{10, 20, 30}, {80, 100, 120}}
</pre></div>

<p>with
</p>
<div class="example">
<pre class="verbatim">ra::Big&lt;double, 2&gt; a = {{1, 2, 3}, {4, 5, 6}};
ra::Big&lt;double, 1&gt; b = {10, 20, 30};
ra::Big&lt;double, 2&gt; c({2, 3}, 0.);
iter&lt;1&gt;(c) = iter&lt;1&gt;(a) * iter&lt;1&gt;(b); // multiply (each item of a) by (b)
</pre><pre class="example">&rArr; a = {{10, 40, 90}, {40, 100, 180}}
</pre></div>

<p>Note that in this case we cannot construct <code>c</code> directly from <code>iter&lt;1&gt;(a) * iter&lt;1&gt;(b)</code>, since the constructor for <code>ra::Big</code> matches its argument using (the equivalent of) <code>iter&lt;0&gt;(*this)</code>. See <a href="#x_002diter"><code>iter</code></a> for more examples.
</p>
<p>Cell iteration is appropriate when the operations take naturally operands of rank &gt; 0; for instance, the operation ‘determinant of a matrix’ is naturally of rank 2. When the operation is of rank 0, such as <code>*</code> above, there may be faster ways to rearrange shapes for matching (see <a href="#The-rank-conjunction">The rank conjunction</a>).
</p>
<p>FIXME More examples.
</p>
<hr>
</div>
<div class="section" id="Slicing">
<div class="header">
<p>
Next: <a href="#Functions" accesskey="n" rel="next">Functions</a>, Previous: <a href="#Cell-iteration" accesskey="p" rel="prev">Cell iteration</a>, Up: <a href="#Usage" accesskey="u" rel="up">Usage</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Slicing-1"></span><h3 class="section">2.6 Slicing</h3>

<p>Slicing is an array extension of the subscripting operation. However, tradition and convenience have given it a special status in most array languages, together with some peculiar semantics that <code>ra::</code> supports.
</p>
<p>The form of the scripting operator <code>A(i₀, i₁, ...)</code> makes it plain that <code>A</code> is a function of <code>rank(A)</code> integer arguments<a id="DOCF8" href="#FOOT8"><sup>8</sup></a>. An array extension is immediately available through <code>map</code>. For example:
</p>
<div class="example">
<pre class="verbatim">ra::Big&lt;double, 1&gt; a = {1., 2., 3., 4.};
ra::Big&lt;int, 1&gt; i = {1, 3};
map(a, i) = 77.;
</pre><pre class="example">&rArr; a = {1., 77., 3, 77.}
</pre></div>

<p>Just as with any use of <code>map</code>, array arguments are subject to the prefix agreement rule.
</p>
<div class="example">
<pre class="verbatim">ra::Big&lt;double, 2&gt; a({2, 2}, {1., 2., 3., 4.});
ra::Big&lt;int, 1&gt; i = {1, 0};
ra::Big&lt;double, 1&gt; b = map(a, i, 0);
</pre><pre class="example">&rArr; b = {3., 1.} // {a(1, 0), a(0, 0)}
</pre></div>

<div class="example">
<pre class="verbatim">ra::Big&lt;int, 1&gt; j = {0, 1};
b = map(a, i, j);
</pre><pre class="example">&rArr; b = {3., 2.} // {a(1, 0), a(0, 1)}
</pre></div>

<p>The latter is a form of sparse subscripting.
</p>
<p>Most array operations (e.g. <code>+</code>) are defined through <code>map</code> in this way. For example, <code>A+B+C</code> is defined as <code>map(+, A, B, C)</code> (or the equivalent <code>map(+, map(+, A, B), C)</code>). Not so for the subscripting operation:
</p>
<div class="example">
<pre class="verbatim">ra::Big&lt;double, 2&gt; A {{1., 2.}, {3., 4.}};
ra::Big&lt;int, 1&gt; i = {1, 0};
ra::Big&lt;int, 1&gt; j = {0, 1};
// {{A(i₀, j₀), A(i₀, j₁)}, {A(i₁, j₀), A(i₁, j₁)}}
ra::Big&lt;double, 2&gt; b = A(i, j);
</pre><pre class="example">&rArr; b = {{3., 4.}, {1., 2.}}
</pre></div>

<span id="x_002dsubscript_002douter_002dproduct"></span><p><code>A(i, j, ...)</code> is defined as the <em>outer product</em> of the indices <code>(i, j, ...)</code> with operator <code>A</code>, because this operation sees much more use in practice than <code>map(A, i, j ...)</code>.
</p>
<span id="index-elision_002c-index"></span>
<p>You may give fewer subscripts than the rank of the array. The full extent is assumed for the missing subscripts (cf <a href="#x_002dall"><code>all</code></a> below):
</p>
<div class="example">
<pre class="verbatim">ra::Big&lt;int, 3&gt; a({2, 2, 2}, {1, 2, 3, 4, 5, 6, 7, 8});
auto a0 = a(0); // same as a(0, ra::all, ra::all)
auto a10 = a(1, 0); // same as a(1, 0, ra::all)
</pre><pre class="example">&rArr; a0 = {{1, 2}, {3, 4}}
&rArr; a10 = {5, 6}
</pre></div>

<p>This supports the notion (see <a href="#Rank-polymorphism">Rank polymorphism</a>) that a 3-array is also an 2-array where the elements are 1-arrays themselves, or a 1-array where the elements are 2-arrays. This important property is directly related to the mechanism of rank extension (see <a href="#Rank-extension">Rank extension</a>).
</p>
<p>Besides, when the subscripts <code>i, j, ...</code> are scalars or integer sequences of the form <code>(o, o+s, ..., o+s*(n-1))</code> (<em>linear ranges</em>), the subscripting can be performed inmediately at constant cost, and without needing to construct an expression object. This optimization is called <a href="#Drag-along-and-beating"><em>beating</em></a>.
</p>
<p><code>ra::</code> isn&rsquo;t smart enough to know when an arbitrary expression might be a linear range, so the following special objects are provided:
</p>
<span id="x_002diota"></span><dl class="def">
<dt id="index-iota"><span class="category">Special&nbsp;object<!-- /@w -->: </span><span><strong>iota</strong> <em>count [start:0 [step:1]]</em><a href='#index-iota' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Create a linear range <code>start, start+step, ... start+step*(count-1)</code>.
</p>
<p>This can used anywhere an array expression is expected.
</p>
<div class="example">
<pre class="verbatim">ra::Big&lt;int, 1&gt; a = ra::iota(4, 3 -2);
</pre><pre class="example">&rArr; a = {3, 1, -1, -3}
</pre></div>

<p>Here, <code>b</code> and <code>c</code> are <code>View</code>s (see <a href="#Containers-and-views">Containers and views</a>).
</p><div class="example">
<pre class="verbatim">ra::Big&lt;int, 1&gt; a = {1, 2, 3, 4, 5, 6};
auto b = a(iota(3));
auto c = a(iota(3, 3));
</pre><pre class="example">&rArr; a = {1, 2, 3}
&rArr; a = {4, 5, 6}
</pre></div>

<span id="index-TensorIndex"></span>
<p><code>iota()</code> by itself is an expression of rank 1 and undefined length. It must be used with other terms whose lengths are defined, so that the overall shape of the array expression can be determined. In general, <code>iota&lt;n&gt;()</code> is an array expression of rank <code>n</code>+1 that represents the <code>n</code>-th index of an array expression. This is similar to Blitz++&rsquo;s <code>TensorIndex</code>.
</p>
<p><code>ra::</code> offers the shortcut <code>ra::_0</code> for <code>ra::iota&lt;0&gt;()</code>, etc.
</p>
<div class="example">
<pre class="verbatim">ra::Big&lt;int, 1&gt; v = {1, 2, 3};
cout &lt;&lt; (v - ra::_0) &lt;&lt; endl; // { 1-0, 2-1, 3-2 }
// cout &lt;&lt; (ra::_0) &lt;&lt; endl; // error: undefined length
// cout &lt;&lt; (v - ra::_1) &lt;&lt; endl; // error: undefined length on axis 1
ra::Big&lt;int, 2&gt; a({3, 2}, 0);
cout &lt;&lt; (a + ra::_0 - ra::_1) &lt;&lt; endl; // {{0, -1, -2}, {1, 0, -1}, {2, 1, 0}}
</pre></div>

<p>When undefined length <code>iota()</code> is used as a subscript by itself, the result isn&rsquo;t a <code>View</code>. This allows <code>view(iota())</code> to match with expressions of different lengths, as in the following example.
</p><div class="example">
<pre class="verbatim">ra::Big&lt;int, 1&gt; a = {1, 2, 3, 4, 5, 6};
ra::Big&lt;int, 1&gt; b = {1, 2, 3};
cout &lt;&lt; (b + a(iota())) &lt;&lt; endl; // a(iota()) is not a View
</pre><pre class="example">-| 3
2 4 6
</pre></div>

<p>Note the difference between
</p><ul>
<li> <code>ra::iota&lt;3&gt;()</code> —
an expression of rank 4 and undefined length, representing a linear sequence over the tensor index of axis 3
</li><li> <code>ra::iota(3)</code> ≡ <code>ra::iota&lt;0&gt;(3)</code> —
an expression of rank 1, representing the sequence <code>0, 1, 2</code>.
</li></ul>

</dd></dl>

<span id="x_002dall"></span><dl class="def">
<dt id="index-all"><span class="category">Special&nbsp;object<!-- /@w -->: </span><span><strong>all</strong><a href='#index-all' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Create a linear range <code>0, 1, ... (nᵢ-1)</code> when used as a subscript at the <var>i</var>-th place of a subscripting expression. This might not be the <var>i</var>-th argument; see <a href="#x_002dinsert"><code>insert</code></a>, <a href="#x_002ddots"><code>dots</code></a>.
</p>
<p>This object cannot stand alone as an array expression. All the examples below result in <code>View</code> objects:
</p>
<div class="example">
<pre class="verbatim">ra::Big&lt;int, 2&gt; a({3, 2}, {1, 2, 3, 4, 5, 6});
auto b = a(ra::all, ra::all); // (1) a view of the whole of a
auto c = a(iota(3), iota(2)); // same as (1)
auto d = a(iota(3), ra::all); // same as (1)
auto e = a(ra:all, iota(2)); // same as (1)
auto f = a(0, ra::all); // first row of a
auto g = a(ra::all, 1); // second column of a
auto g = a(ra::all, ra::dots&lt;0&gt;, 1); // same
</pre></div>

<p><code>all</code> is a special case (<code>dots&lt;1&gt;</code>) of the more general object <code>dots</code>.
</p>
</dd></dl>

<span id="x_002ddots"></span><dl class="def">
<dt id="index-dots_003cn_003e"><span class="category">Special&nbsp;object<!-- /@w -->: </span><span><strong>dots&lt;n&gt;</strong><a href='#index-dots_003cn_003e' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Equivalent to as many instances of <code>ra::all</code> as indicated by <code>n</code>, which must not be negative. Each instance takes the place of one argument to the subscripting operation.
</p>
<p>If <var>n</var> is defaulted (<code>dots&lt;&gt;</code>), all available places will be used; this can only be done once in a given subscript list.
</p>
<p>This object cannot stand alone as an array expression. All the examples below result in <code>View</code> objects:
</p>
<div class="example">
<pre class="verbatim">ra::Big&lt;int, 3&gt; a({3, 2, 4}, ...);
auto h = a(); // all of a

auto b = a(ra::all, ra::all, ra::all); // (1) all of a
auto c = a(ra::dots&lt;3&gt;); // same as (1)
auto d = a(ra::all, ra::dots&lt;2&gt;); // same as (1)
auto e = a(ra::dots&lt;2&gt;, ra::all); // same as (1)
auto f = a(ra::dots&lt;&gt;); // same as (1)

auto j0 = a(0, ra::dots&lt;2&gt;); // first page of a
auto j1 = a(0); // same
auto j2 = a(0, ra::dots&lt;&gt;); // same

auto k0 = a(ra::all, 0); // first row of a
auto k1 = a(ra::all, 0, ra::all); // same
auto k2 = a(ra::all, 0, ra::dots&lt;&gt;); // same
auto k3 = a(ra::dots&lt;&gt;, 0, ra::all); // same
// auto k = a(ra::dots&lt;&gt;, 0, ra::dots&lt;&gt;); // error

auto l0 = a(ra::all, ra::all, 0); // first column of a
auto l1 = a(ra::dots&lt;2&gt;, 0); // same
auto l2 = a(ra::dots&lt;&gt;, 0); // same
</pre></div>

<p>This is useful when writing rank-generic code, see <code>examples/maxwell.cc</code> in the distribution for an example.
</p>
</dd></dl>

<p>The following special objects aren&rsquo;t related to linear ranges, but they are meant to be used in a subscript context. Using them in other contexts will result in a compile time error.
</p>
<span id="index-len"></span>
<span id="index-end_002c-Octave_002fMatlab"></span>
<span id="x_002dlen"></span><dl class="def">
<dt id="index-len-1"><span class="category">Special&nbsp;object<!-- /@w -->: </span><span><strong>len</strong><a href='#index-len-1' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Represents the length of the <var>i</var>-th axis of a subscripted expression, when used at the <var>i</var>-th place of a subscripting expression.
</p>
<p>This works like <code>end</code> in Octave/Matlab, but note that <code>ra::</code> indices begin at 0, so the last element of a vector <code>a</code> is <code>a(ra::len-1)</code>.
</p>
<div class="example">
<pre class="verbatim">ra::Big&lt;int, 2&gt; a({10, 10}, 100 + ra::_0 - ra::_1);
auto a0 = a(ra::len-1); // last row of a; ra::len is a.len(0)
auto a1 = a(ra::all, ra::len-1); // last column a; ra::len is a.len(1)
auto a2 = a(ra::len-1, ra::len-1); // last element of last row; the first ra::len is a.len(0) and the second one is a.len(1)
auto a3 = a(ra::all, ra::iota(2, ra::len-2)); // last two columns of a
auto a4 = a(ra::iota(ra::len/2, 1, 2)); // odd rows of a
a(ra::len - std::array {1, 3, 4}) = 0; // set to 0 the 1st, 3rd and 4th rows of a, counting from the end
</pre></div>

<div class="example">
<pre class="verbatim">ra::Big&lt;int, 3&gt; b({2, 3, 4}, ...);
auto b0 = b(ra::dots&lt;2&gt;, ra::len-1); // ra::len is a.len(2)
auto b1 = b(ra::insert&lt;1&gt;, ra::len-1); // ra::len is a.len(0)
</pre></div>

</dd></dl>

<span id="index-insert"></span>
<span id="x_002dinsert"></span><dl class="def">
<dt id="index-insert_003cn_003e"><span class="category">Special&nbsp;object<!-- /@w -->: </span><span><strong>insert&lt;n&gt;</strong><a href='#index-insert_003cn_003e' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Inserts <code>n</code> new axes at the subscript position. <code>n</code> must not be negative.
</p>
<p>The new axes have step 0 and undefined length, so they will match any length on those axes by repeating items. <code>insert</code> objects cannot stand alone as an array expression. The examples below result in <code>View</code> objects:
</p>
<div class="example">
<pre class="verbatim">auto h = a(insert&lt;0&gt;); // same as (1)
auto k = a(insert&lt;1&gt;); // shape [undefined, 3, 2]
</pre></div>

<span id="index-broadcasting_002c-singleton_002c-Numpy"></span>
<p><code>insert&lt;n&gt;</code> main use is to prepare arguments for broadcasting. In other array systems (e.g. Numpy) broadcasting is done with singleton dimensions, that is, dimensions of length one match dimensions of any length. In <code>ra::</code> singleton dimensions aren&rsquo;t special, so broadcasting requires the use of <code>insert</code>. For example: </p>
<div class="example">
<pre class="verbatim">ra::Big&lt;int, 1&gt; x = {1, 10};
// match shapes [2, U, U] with [U, 3, 2] to produce [2, 3, 2]
cout &lt;&lt; x(ra::all, ra::insert&lt;2&gt;) * a(insert&lt;1&gt;) &lt;&lt; endl;
</pre><pre class="example">-| 2 3 2
1 2
3 4
5 6

10 20
30 40
50 60
</pre></div>

</dd></dl>

<p>We were speaking earlier of the outer product of the subscripts with operator <code>A</code>. Here&rsquo;s a way to perform the actual outer product (with operator <code>*</code>) of two <code>Views</code>, through broadcasting. All three lines are equivalent. See <a href="#x_002dfrom"><code>from</code></a> for a more general way to compute outer products.
</p><div class="example">
<pre class="verbatim">cout &lt;&lt; (A(ra::dots&lt;A.rank()&gt;, ra::insert&lt;B.rank()&gt;) * B(ra::insert&lt;A.rank()&gt;, ra::dots&lt;B.rank()&gt;)) &lt;&lt; endl;
cout &lt;&lt; (A(ra::dots&lt;&gt;, ra::insert&lt;B.rank()&gt;) * B(ra::insert&lt;A.rank()&gt;, ra::dots&lt;&gt;)) &lt;&lt; endl; // default dots&lt;&gt;
cout &lt;&lt; (A * B(ra::insert&lt;A.rank()&gt;)) &lt;&lt; endl; // index elision + prefix matching
</pre></div>

<ul class="section-toc">
<li><a href="#Subscripting-and-rank_002d0-views" accesskey="1">Subscripting and rank-0 views</a></li>
</ul>
<div class="subsection" id="Subscripting-and-rank_002d0-views">
<h4 class="subsection">2.6.1 Subscripting and rank-0 views</h4>

<span id="index-view_002c-rank-0"></span>
<span id="index-rank_002c-runtime"></span>
<span id="index-rank_002c-compile_002dtime"></span>
<p>When the result of the subscripting operation would have rank 0, the type returned is the type of the view <em>element</em> and not a rank-0 view as long as the rank of the result can be determined at compile time. When that&rsquo;s not possible (for instance, the subscripted view has rt rank) then a rank-0 view is returned instead. An automatic conversion is defined for rank-0 views, but manual conversion may be needed in some contexts.
</p>
<div class="example">
<pre class="verbatim">using T = std::complex&lt;double&gt;;
int f(T &amp;);
Big&lt;T, 2&gt; a({2, 3}, 0); // ct rank
Big&lt;T&gt; b({2, 3}, 0); // rt rank

cout &lt;&lt; a(0, 0).real_part() &lt;&lt; endl; // ok, a(0, 0) returns complex &amp;
// cout &lt;&lt; b(0, 0).real_part() &lt;&lt; endl; // error, View&lt;T&gt; has no member real_part
cout &lt;&lt; ((T &amp;)(b(0, 0))).real_part() &lt;&lt; endl; // ok, manual conversion to T &amp;

cout &lt;&lt; f(b(0, 0)) &lt;&lt; endl; // ok, automatic conversion from View&lt;T&gt; to T &amp;
// cout &lt;&lt; f(a(0)) &lt;&lt; endl; // compile time error, conversion failed since ct rank of a(0) is not 0
// cout &lt;&lt; f(b(0)) &lt;&lt; endl; // runtime error, conversion failed since rt rank of b(0) is not 0
</pre></div>


<hr>
</div>
</div>
<div class="section" id="Functions">
<div class="header">
<p>
Next: <a href="#The-rank-conjunction" accesskey="n" rel="next">The rank conjunction</a>, Previous: <a href="#Slicing" accesskey="p" rel="prev">Slicing</a>, Up: <a href="#Usage" accesskey="u" rel="up">Usage</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Functions-1"></span><h3 class="section">2.7 Functions</h3>

<p>You don&rsquo;t need to use <a href="#Array-operations"><code>map</code></a> every time you want to do something with arrays in <code>ra::</code>. A number of array functions are already defined.
</p>
<span id="x_002dscalar_002dops"></span><ul class="section-toc">
<li><a href="#Standard-scalar-operations" accesskey="1">Standard scalar operations</a></li>
<li><a href="#Conditional-operations" accesskey="2">Conditional operations</a></li>
<li><a href="#Special-operations" accesskey="3">Special operations</a></li>
<li><a href="#Elementwise-reductions" accesskey="4">Elementwise reductions</a></li>
<li><a href="#Special-reductions" accesskey="5">Special reductions</a></li>
<li><a href="#Shortcut-reductions" accesskey="6">Shortcut reductions</a></li>
</ul>
<div class="subsection" id="Standard-scalar-operations">
<h4 class="subsection">2.7.1 Standard scalar operations</h4>

<p><code>ra::</code> defines array extensions for <code>+</code>, <code>-</code> (both unary and binary), <code>*</code>, <code>/</code>, <code>!</code>, <code>&amp;&amp;</code>, <code>||</code><a id="DOCF9" href="#FOOT9"><sup>9</sup></a>, <code>&gt;</code>, <code>&lt;</code>, <code>&gt;=</code>, <code>&lt;=</code>, <code>&lt;=&gt;</code>, <code>==</code>, <code>!=</code>, <code>pow</code>, <code>sqr</code>, <code>abs</code>, <code>cos</code>, <code>sin</code>, <code>exp</code>, <code>expm1</code>, <code>sqrt</code>, <code>log</code>, <code>log1p</code>, <code>log10</code>, <code>isfinite</code>, <code>isnan</code>, <code>isinf</code>, <code>max</code>, <code>min</code>, <code>asin</code>, <code>acos</code>, <code>atan</code>, <code>atan2</code>, <code>cosh</code>, <code>sinh</code>, <code>tanh</code>, and <code>lerp</code>.
Extending other scalar operations is straightforward; see <a href="#x_002dnew_002darray_002doperations">New array operations</a>. <code>ra::</code> also defines (and extends) the non-standard functions <code>odd</code>, <a href="#x_002dsqr"><code>sqr</code></a>, <a href="#x_002dsqrm"><code>sqrm</code></a>, <a href="#x_002dconj"><code>conj</code></a>, <a href="#x_002drel_002derror"><code>rel_error</code></a>, and <a href="#x_002dxI"><code>xI</code></a>.
</p>
<p>For example:
</p><div class="example">
<pre class="verbatim">cout &lt;&lt; exp(ra::Small&lt;double, 3&gt; {4, 5, 6}) &lt;&lt; endl;
</pre><pre class="example">  -| 54.5982 148.413 403.429
</pre></div>

</div>
<div class="subsection" id="Conditional-operations">
<h4 class="subsection">2.7.2 Conditional operations</h4>

<p><a href="#x_002dmap"><code>map</code></a> evaluates all of its arguments before passing them along to its operator. This isn&rsquo;t always what you want. The simplest example is <code>where(condition, iftrue, iffalse)</code>, which returns an expression that will evaluate <code>iftrue</code> when <code>condition</code> is true and <code>iffalse</code> otherwise.
</p>
<div class="example">
<pre class="verbatim">ra::Big&lt;double&gt; x ...
ra::Big&lt;double&gt; y = where(x&gt;0, expensive_expr_1(x), expensive_expr_2(x));
</pre></div>

<p>Here <code>expensive_expr_1</code> and <code>expensive_expr_2</code> are array expressions. So the computation of the other arm would be wasted if one were to do instead
</p>
<div class="example">
<pre class="verbatim">ra::Big&lt;double&gt; y = map([](auto &amp;&amp; w, auto &amp;&amp; t, auto &amp;&amp; f) -&gt; decltype(auto) { return w ? t : f; }
                        x&gt;0, expensive_expr_1(x), expensive_function_2(x));
</pre></div>

<p>If the expressions have side effects, then <code>map</code> won&rsquo;t even give the right result.
</p>
<div class="example">
<pre class="verbatim">ra::Big&lt;int, 1&gt; o = {};
ra::Big&lt;int, 1&gt; e = {};
ra::Big&lt;int, 1&gt; n = {1, 2, 7, 9, 12};
ply(where(odd(n), map([&amp;o](auto &amp;&amp; x) { o.push_back(x); }, n), map([&amp;e](auto &amp;&amp; x) { e.push_back(x); }, n)));
cout &lt;&lt; &quot;o: &quot; &lt;&lt; ra::noshape &lt;&lt; o &lt;&lt; &quot;, e: &quot; &lt;&lt; ra::noshape &lt;&lt; e &lt;&lt; endl;
</pre><pre class="example">-| o: 1 7 9, e: 2 12
</pre></div>

<p>FIXME Artificial example.
FIXME Do we want to expose ply(); this is the only example in the manual that uses it.
</p>
<p>When the choice is between more than two expressions, there&rsquo;s <a href="#x_002dpick"><code>pick</code></a>, which operates similarly, but accepts an integer instead of a boolean selector.
</p>
</div>
<div class="subsection" id="Special-operations">
<h4 class="subsection">2.7.3 Special operations</h4>

<p>Some operations are essentially scalar operations, but require special syntax and would need a lambda wrapper to be used with <code>map</code>. <code>ra::</code> comes with a few of these already defined.
</p>
<p>FIXME
</p>
</div>
<div class="subsection" id="Elementwise-reductions">
<h4 class="subsection">2.7.4 Elementwise reductions</h4>

<p><code>ra::</code> defines the whole-array one-argument reductions <code>any</code>, <code>every</code>, <code>amax</code>, <code>amin</code>, <code>sum</code>, <code>prod</code> and the two-argument reductions <code>dot</code> and <code>cdot</code>. Note that <code>max</code> and <code>min</code> are two-argument scalar operations with array extensions, while <code>amax</code> and <code>amin</code> are reductions. <code>any</code> and <code>every</code> are short-circuiting.
</p>
<p>You can define reductions the same way <code>ra::</code> does:
</p>
<div class="example">
<pre class="verbatim">template &lt;class A&gt;
inline auto op_reduce(A &amp;&amp; a)
{
    T c = op_default;
    for_each([&amp;c](auto &amp;&amp; a) { c = op(c, a); }, a);
    return c;
}
</pre></div>

<p>Often enough you need to reduce over particular axes. This is possible by combining assignment operators with the <a href="#Rank-extension">rank extension</a> mechanism, or using the <a href="#The-rank-conjunction">rank conjunction</a>, or iterating over <a href="#Cell-iteration">cells of higher rank</a>. For example:
</p>
<div class="example">
<pre class="verbatim">    ra::Big&lt;double, 2&gt; a({m, n}, ...);

    ra::Big&lt;double, 1&gt; sum_rows({n}, 0.);
    iter&lt;1&gt;(sum_rows) += iter&lt;1&gt;(a);
    // for_each(ra::wrank&lt;1, 1&gt;([](auto &amp; c, auto &amp;&amp; a) { c += a; }), sum_rows, a) // alternative
    // sum_rows += transpose&lt;1, 0&gt;(a); // another

    ra::Big&lt;double, 1&gt; sum_cols({m}, 0.);
    sum_cols += a;
</pre></div>

<p>FIXME example with assignment op
</p>
<p>A few common operations of this type are already packaged in <code>ra::</code>.
</p>
</div>
<div class="subsection" id="Special-reductions">
<h4 class="subsection">2.7.5 Special reductions</h4>

<p><code>ra::</code> defines the following special reductions.
</p>
<p>FIXME
</p>
</div>
<div class="subsection" id="Shortcut-reductions">
<h4 class="subsection">2.7.6 Shortcut reductions</h4>

<p>Some reductions do not need to traverse the whole array if a certain condition is encountered early. The most obvious ones are the reductions of <code>&amp;&amp;</code> and <code>||</code>, which <code>ra::</code> defines as <code>every</code> and <code>any</code>.
</p>
<p>FIXME
</p>
<p>These operations are defined on top of another function <code>early</code>.
</p>
<p>FIXME early
</p>
<p>The following is often useful.
</p>
<p>FIXME lexicographical compare etc.
</p>
<hr>
</div>
</div>
<div class="section" id="The-rank-conjunction">
<div class="header">
<p>
Next: <a href="#Compatibility" accesskey="n" rel="next">Compatibility</a>, Previous: <a href="#Functions" accesskey="p" rel="prev">Functions</a>, Up: <a href="#Usage" accesskey="u" rel="up">Usage</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
</div>
<span id="The-rank-conjunction-1"></span><h3 class="section">2.8 The rank conjunction</h3>

<p>We have seen in <a href="#Cell-iteration">Cell iteration</a> that it is possible to treat an r-array as an array of lower rank with subarrays as its elements. With the <a href="#x_002diter"><code>iter&lt;cell rank&gt;</code></a> construction, this ‘exploding’ is performed (notionally) on the argument; the operation of the array expression is applied blindly to these cells, whatever they turn out to be.
</p>
<div class="example">
<pre class="verbatim">for_each(my_sort, iter&lt;1&gt;(A)); // (in ra::) my_sort is a regular function, cell rank must be given
for_each(my_sort, iter&lt;0&gt;(A)); // (in ra::) error, bad cell rank
</pre></div>

<p>The array language J has instead the concept of <em>verb rank</em>. Every function (or <em>verb</em>) has an associated cell rank for each of its arguments. Therefore <code>iter&lt;cell rank&gt;</code> is not needed.
</p>
<div class="example">
<pre class="verbatim">for_each(sort_rows, A); // (not in ra::) will iterate over 1-cells of A, sort_rows knows
</pre></div>

<p><code>ra::</code> doesn&rsquo;t have ‘verb ranks’ yet. In practice one can think of <code>ra::</code>&rsquo;s operations as having a verb rank of 0. However, <code>ra::</code> supports a limited form of J&rsquo;s <em>rank conjunction</em> with the function <a href="#x_002dwrank"><code>wrank</code></a>.
</p>
<p>This is an operator that takes one verb (such operators are known as <em>adverbs</em> in J) and produces another verb with different ranks. These ranks are used for rank extension through prefix agreement, but then the original verb is used on the cells that result. The rank conjunction can be nested, and this allows repeated rank extension before the innermost operation is applied.
</p>
<p>A standard example is ‘outer product’.
</p>
<div class="example">
<pre class="verbatim">ra::Big&lt;int, 1&gt; a = {1, 2, 3};
ra::Big&lt;int, 1&gt; b = {40, 50};
ra::Big&lt;int, 2&gt; axb = map(ra::wrank&lt;0, 1&gt;([](auto &amp;&amp; a, auto &amp;&amp; b) { return a*b; }),
                            a, b)
</pre><pre class="example">&rArr; axb = {{40, 80, 120}, {50, 100, 150}}
</pre></div>

<p>It works like this. The verb <code>ra::wrank&lt;0, 1&gt;([](auto &amp;&amp; a, auto &amp;&amp; b) { return a*b; })</code> has verb ranks (0, 1), so the 0-cells of <code>a</code> are paired with the 1-cells of <code>b</code>. In this case <code>b</code> has a single 1-cell. The frames and the cell shapes of each operand are:
</p>
<div class="example">
<pre class="verbatim">a: 3 |
b:   | 2
</pre></div>

<p>Now the frames are rank-extended through prefix agreement.
</p>
<div class="example">
<pre class="verbatim">a: 3 |
b: 3 | 2
</pre></div>

<p>Now we need to perform the operation on each cell. The verb <code>[](auto &amp;&amp; a, auto &amp;&amp; b) { return a*b; }</code> has verb ranks (0, 0). This results in the 0-cells of <code>a</code> (which have shape ()) being rank-extended to the shape of the 1-cells of <code>b</code> (which is (2)).
</p>
<div class="example">
<pre class="verbatim">a: 3 | 2
b: 3 | 2
</pre></div>

<p>This use of the rank conjunction is packaged in <code>ra::</code> as the <a href="#x_002dfrom"><code>from</code></a> operator. It supports any number of arguments, not only two.
</p>
<div class="example">
<pre class="verbatim">ra::Big&lt;int, 1&gt; a = {1, 2, 3};
ra::Big&lt;int, 1&gt; b = {40, 50};
ra::Big&lt;int, 2&gt; axb = from([](auto &amp;&amp; a, auto &amp;&amp; b) { return a*b; }), a, b)
</pre><pre class="example">&rArr; axb = {{40, 80, 120}, {50, 100, 150}}
</pre></div>

<p>Another example is matrix multiplication. For 2-array arguments C, A and B with shapes C: (m, n) A: (m, p) and B: (p, n), we want to perform the operation C(i, j) += A(i, k)*B(k, j). The axis alignment gives us the ranks we need to use.
</p>
<div class="example">
<pre class="verbatim">C: m |   | n
A: m | p |
B:   | p | n
</pre></div>

<p>First we&rsquo;ll align the first axes of C and A using the cell ranks (1, 1, 2). The cell shapes are:
</p>
<div class="example">
<pre class="verbatim">C: m | n
A: m | p
B:   | p n
</pre></div>

<p>Then we&rsquo;ll use the ranks (1, 0, 1) on the cells:
</p>
<div class="example">
<pre class="verbatim">C: m |   | n
A: m | p |
B:   | p | n
</pre></div>

<p>The final operation is a standard operation on arrays of scalars. In actual <code>ra::</code> syntax:
</p>
<div class="example">
<pre class="verbatim">ra::Big A({3, 2}, {1, 2, 3, 4, 5, 6});
ra::Big B({2, 3}, {7, 8, 9, 10, 11, 12});
ra::Big C({3, 3}, 0.);
for_each(ra::wrank&lt;1, 1, 2&gt;(ra::wrank&lt;1, 0, 1&gt;([](auto &amp;&amp; c, auto &amp;&amp; a, auto &amp;&amp; b) { c += a*b; })), C, A, B);
</pre><pre class="example">&rArr; C = {{27, 30, 33}, {61, 68, 75}, {95, 106, 117}}
</pre></div>

<p>Note that <code>wrank</code> cannot be used to transpose axes in general.
</p>
<p>I hope that in the future something like <code>C(i, j) += A(i, k)*B(k, j)</code>, where <code>i, j, k</code> are special objects, can be automatically translated to the requisite combination of <code>wrank</code> and perhaps also <a href="#x_002dtranspose"><code>transpose</code></a>. For the time being, you have to align or transpose the axes yourself.
</p>
<hr>
</div>
<div class="section" id="Compatibility">
<div class="header">
<p>
Next: <a href="#Extension" accesskey="n" rel="next">Extension</a>, Previous: <a href="#The-rank-conjunction" accesskey="p" rel="prev">The rank conjunction</a>, Up: <a href="#Usage" accesskey="u" rel="up">Usage</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Compatibility-1"></span><h3 class="section">2.9 Compatibility</h3>

<ul class="section-toc">
<li><a href="#Using-other-C-and-C_002b_002b-types-with-ra_003a_003a" accesskey="1">Using other C and C++ types with <code>ra::</code></a></li>
<li><a href="#Using-ra_003a_003a-types-with-the-STL" accesskey="2">Using <code>ra::</code> types with the STL</a></li>
<li><a href="#Using-ra_003a_003a-types-with-other-libraries" accesskey="3">Using <code>ra::</code> types with other libraries</a></li>
</ul>
<div class="subsection" id="Using-other-C-and-C_002b_002b-types-with-ra_003a_003a">
<h4 class="subsection">2.9.1 Using other C and C++ types with <code>ra::</code></h4>

<span id="index-foreign-type"></span>
<span id="x_002dforeign_002dtype"></span><p><code>ra::</code> accepts certain types from outside <code>ra::</code> (<em>foreign types</em>) as array expressions. Generally it is enough to mix the foreign type with a type from <code>ra::</code> and let deduction work.
</p>
<div class="example">
<pre class="verbatim">std::vector&lt;int&gt; x = {1, 2, 3};
ra::Small&lt;int, 3&gt; y = {6, 5, 4};
cout &lt;&lt; (x-y) &lt;&lt; endl;
</pre><pre class="example">-| -5 -3 -1
</pre></div>

<span id="index-start"></span>
<p>Foreign types can be brought into <code>ra::</code> explicitly with the function <a href="#x_002dstart"><code>start</code></a>.
</p>
<div class="example">
<pre class="verbatim">std::vector&lt;int&gt; x = {1, 2, 3};
// cout &lt;&lt; sum(x) &lt;&lt; endl; // error, sum not found
cout &lt;&lt; sum(ra::start(x)) &lt;&lt; endl;
cout &lt;&lt; ra::sum(x) &lt;&lt; endl;
</pre><pre class="example">-| 6
</pre></div>

<p>The following types are accepted as foreign types:
</p>
<ul>
<li> Built-in arrays <span id="index-built_002din-array"></span>
produce an expression of positive rank and ct size.
</li><li> <code>std::array</code>
produces an expression of rank 1 and ct size.
</li><li> Other types conforming to <code>std::ranges::random_access_range</code>, including <code>std::vector</code>, <code>std::string</code>, etc.
produce an expression of rank 1 and rt size.
</li></ul>

<p>Raw pointers must be brought into <code>ra::</code> explicitly using the function <a href="#x_002dptr"><code>ptr</code></a>, which produces an expression of rank 1 and <em>undefined</em> size.
</p>
<p>Compare:
</p>
<div class="example">
<pre class="verbatim">int p[] = {1, 2, 3};
int * z = p;
ra::Big&lt;int, 1&gt; q {1, 2, 3};
q += p; // ok, q is ra::, p is foreign object with shape info
ra::start(p) += q; // can't redefine operator+=(int[]), foreign needs ra::start()
// z += q; // error: raw pointer needs ra::ptr()
ra::ptr(z) += p; // ok, shape is determined by foreign object p
</pre></div>

<span id="x_002dis_002dscalar"></span><p>Some types are accepted automatically as scalars. These include non-pointer types for which <code>std::is_scalar_v</code> is true, like <code>char</code>, <code>int</code>, <code>double</code>, etc. as well as <code>std::complex&lt;T&gt;</code>. You can add your own types as scalar types with the following declaration:
</p>
<pre class="verbatim">    namespace ra { template &lt;&gt; constexpr bool is_scalar_def&lt;MYTYPE&gt; = true; }
</pre>
<p>Otherwise, you can bring a scalar object into <code>ra::</code> on the spot, with the function <a href="#x_002dscalar"><code>scalar</code></a>.
</p>
</div>
<div class="subsection" id="Using-ra_003a_003a-types-with-the-STL">
<h4 class="subsection">2.9.2 Using <code>ra::</code> types with the STL</h4>

<p>General <code>ra::</code> <a href="#Containers-and-views">views</a> provide STL compatible <code>ForwardIterator</code>s through the members <code>begin()</code> and <code>end()</code>. These iterators traverse the elements of the array (0-cells) in row major order, regardless of the storage order of the view.
</p>
<p>For <a href="#Containers-and-views">containers</a> <code>begin()</code> provides <code>RandomAccessIterator</code>s, which is handy for certain functions such as <code>sort</code>. There&rsquo;s no reason why all views couldn&rsquo;t provide <code>RandomAccessIterator</code>, but these wouldn&rsquo;t be efficient for ranks above 1, and I haven&rsquo;t implemented them. The container <code>RandomAccessIterator</code>s that are provided are in fact raw pointers.
</p>
<div class="example">
<pre class="verbatim">ra::Big&lt;int&gt; x {3, 2, 1}; // x is a Container
auto y = x(); // y is a View on x
// std::sort(y.begin(), y.end()); // error: y.begin() is not RandomAccessIterator
std::sort(x.begin(), x.end()); // ok, we know x is stored in row-major order
</pre><pre class="example">&rArr; x = {1, 2, 3}
</pre></div>

<span id="index-other-libraries_002c-interfacing-with"></span>
</div>
<div class="subsection" id="Using-ra_003a_003a-types-with-other-libraries">
<h4 class="subsection">2.9.3 Using <code>ra::</code> types with other libraries</h4>

<p>When you have to pass arrays back and forth between your program and an external library, perhaps even another language, it is necessary for both sides to agree on memory layout. <code>ra::</code> gives you access to its own memory layout and allows you to obtain an <code>ra::</code> view on any type of memory.
</p>
<ul class="section-toc">
<li><a href="#The-good-array-citizen" accesskey="1">The good array citizen</a></li>
<li><a href="#The-bad-array-citizen" accesskey="2">The bad array citizen</a></li>
</ul>
<div class="subsubsection" id="The-good-array-citizen">
<h4 class="subsubsection">2.9.3.1 The good array citizen</h4>


<span id="index-BLIS"></span>
<p>The good array citizen describes its arrays with the same parameters as <code>ra::</code>: a pointer, plus a length and a step per dimension. You don&rsquo;t have to worry about special cases. Say <a href="https://github.com/flame/blis">BLIS</a>:
</p>
<blockquote>
<pre class="verbatim">#include &lt;blis.h&gt;

ra::Big&lt;double, 2&gt; A({M, K}, ...);
ra::Big&lt;double, 2&gt; B({K, N}, ...);
ra::Big&lt;double, 2&gt; C({M, N}, ...);
double alpha = ...;
double beta = ...;

// C := βC + αAB
bli_dgemm(BLIS_NO_TRANSPOSE, BLIS_NO_TRANSPOSE, M, N, K, &amp;alpha,
          A.data(), A.step(0), A.step(1),
          B.data(), B.step(0), B.step(1),
          &amp;beta, C.data(), C.step(0), C.step(1));
</pre></blockquote>

<span id="index-FFTW"></span>
<p>Another good array citizen, <a href="http://fftw.org">FFTW</a> handles arbitrary rank:
</p>
<blockquote>
<pre class="verbatim">#include &lt;fftw3.h&gt;

...

using complex = std::complex&lt;double&gt;;
static_assert(sizeof(complex)==sizeof(fftw_complex));

// forward DFT over the last r axes of a -&gt; b
void fftw(int r, ra::View&lt;complex&gt; const a, ra::View&lt;complex&gt; b)
{
    int const rank = a.rank();
    assert(r&gt;0 &amp;&amp; r&lt;=rank);
    assert(every(ra::start(shape(a))==shape(b)));
    fftw_iodim dims[r];
    fftw_iodim howmany_dims[rank-r];
    for (int i=0; i!=rank; ++i) {
        if (i&gt;=rank-r) {
            dims[i-rank+r].n = a.len(i);
            dims[i-rank+r].is = a.step(i);
            dims[i-rank+r].os = b.step(i);
        } else {
            howmany_dims[i].n = a.len(i);
            howmany_dims[i].is = a.step(i);
            howmany_dims[i].os = b.step(i);
        }
    }
    fftw_plan p;
    p = fftw_plan_guru_dft(r, dims, rank-r, howmany_dims,
                           (fftw_complex *)(a.data()), (fftw_complex *)(b.data()),
                           FFTW_FORWARD, FFTW_ESTIMATE);
    fftw_execute(p);
    fftw_destroy_plan(p);
}
</pre></blockquote>

<span id="index-Guile"></span>
<p>Translating array descriptors from a foreign language should be fairly simple. For example, this is how to convert a <a href="https://www.gnu.org/software/guile/manual/html_node/Accessing-Arrays-from-C.html#Accessing-Arrays-from-C">Guile</a> array view into an <code>ra::</code> view:
</p>
<blockquote>
<pre class="verbatim">    SCM a; // say a is #nf64(...)

    ...

    scm_t_array_handle h;
    scm_array_get_handle(a, &amp;h);
    scm_t_array_dim const * dims = scm_array_handle_dims(&amp;h);
    View&lt;double&gt; v(map([](int i) { return ra::Dim { dims[i].ubnd-dims[i].lbnd+1, dims[i].inc }; },
                       ra::iota(scm_array_handle_rank(&amp;h))),
                   scm_array_handle_f64_writable_elements(&amp;h));

    ...

    scm_array_handle_release(&amp;h);
</pre></blockquote>

<span id="index-Numpy-2"></span>
<span id="index-Python"></span>
<p>Numpy&rsquo;s C API has the type <a href="https://docs.scipy.org/doc/numpy/reference/c-api.array.html"><code>PyArrayObject</code></a> which can be used in the same way as Guile&rsquo;s <code>scm_t_array_handle</code> in the example above.
</p>
<p>It is usually simpler to let the foreign language handle the memory, even though there should be ways to transfer ownership (e.g. Guile has <a href="https://www.gnu.org/software/guile/manual/html_node/SRFI_002d4-API.html#index-scm_005ftake_005ff64vector"><code>scm_take_xxx</code></a>).
</p>
</div>
<div class="subsubsection" id="The-bad-array-citizen">
<h4 class="subsubsection">2.9.3.2 The bad array citizen</h4>

<p>Unfortunately there are many libraries that don&rsquo;t accept arbitrary array parameters, or that do strange things with particular values of lengths and/or steps.
</p>
<p>The most common case is that a library doesn&rsquo;t handle steps at all, and it only accepts unit step for rank 1 arrays, or packed row-major or column-major storage for higher rank arrays. In that case, you might be forced to copy your array before passing it along.
</p>

<p>Other libraries do accept steps, but not arbitrary ones. For example <a href="https://www.netlib.org/blas">https://www.netlib.org/blas</a>&rsquo; <code>cblas_dgemm</code> has this prototype:
</p>
<blockquote>
<pre class="verbatim">cblas_dgemm(order, transA, transB, m, n, k, alpha, A, lda, B, ldb, beta, C, ldc);
</pre></blockquote>

<p><code>A</code>, <code>B</code>, <code>C</code> are (pointers to) 2-arrays, but the routine accepts only one step argument for each (<code>lda</code>, etc.). CBLAS also doesn&rsquo;t understand <code>lda</code> as a arbitrary step, but rather as the dimension of a larger array that you&rsquo;re slicing <code>A</code> from, and some implementations will mishandle negative or zero <code>lda</code>.
</p>
<p>Sometimes you can work around this by fiddling with <code>transA</code> and <code>transB</code>, but in general you need to check your array parameters and you may need to make copies.
</p>
<span id="index-OpenGL"></span>
<p>OpenGL is another library that requires <a href="https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glVertexAttribPointer.xhtml">contortions:</a>
</p>
<blockquote>
<pre class="verbatim">void glVertexAttribPointer(GLuint index,
                           GLint size,
                           GLenum type,
                           GLboolean normalized,
                           GLsizei step,
                           const GLvoid * pointer);
</pre>
<p>[...]
</p>
<p><em>step</em>
</p>
<blockquote>
<p>Specifies the byte offset between consecutive generic vertex attributes. If step is 0, the generic vertex attributes are understood to be tightly packed in the array. The initial value is 0.
    </p></blockquote>
</blockquote>

<p>It isn&rsquo;t clear whether negative steps are legal, either. So just as with CBLAS, passing arbitrary array views may require copies.
</p>
<hr>
</div>
</div>
</div>
<div class="section" id="Extension">
<div class="header">
<p>
Next: <a href="#Error-handling" accesskey="n" rel="next">Error handling</a>, Previous: <a href="#Compatibility" accesskey="p" rel="prev">Compatibility</a>, Up: <a href="#Usage" accesskey="u" rel="up">Usage</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Extension-1"></span><h3 class="section">2.10 Extension</h3>

<ul class="section-toc">
<li><a href="#New-scalar-types" accesskey="1">New scalar types</a></li>
<li><a href="#New-array-operations" accesskey="2">New array operations</a></li>
</ul>
<div class="subsection" id="New-scalar-types">
<h4 class="subsection">2.10.1 New scalar types</h4>

<p><code>ra::</code> will let you construct arrays of arbitrary types out of the box. This is the same functionality you get with e.g. <code>std::vector</code>.
</p>
<div class="example">
<pre class="verbatim">struct W { int x; }
ra::Big&lt;W, 2&gt; w = {{ {4}, {2} }, { {1}, {3} }};
cout &lt;&lt; W(1, 1).x &lt;&lt; endl;
cout &lt;&lt; amin(map([](auto &amp;&amp; x) { return w.x; }, w)) &lt;&lt; endl;
</pre><pre class="example">-| 3
   1
</pre></div>

<p>However, if you want to mix arbitrary types in array operations, you&rsquo;ll need to tell <code>ra::</code> that that is actually what you want. This is to avoid conflicts with other libraries.
</p>
<div class="example">
<pre class="verbatim">namespace ra { template &lt;&gt; constexpr bool is_scalar_def&lt;W&gt; = true; }
...
W ww {11};
for_each([](auto &amp;&amp; x, auto &amp;&amp; y) { cout &lt;&lt; (x.x + y.y) &lt;&lt; &quot; &quot;; }, w, ww); // ok
</pre><pre class="example">-| 15 13 12 14
</pre></div>

<p>but
</p>
<div class="example">
<pre class="verbatim">struct U { int x; }
U uu {11};
for_each([](auto &amp;&amp; x, auto &amp;&amp; y) { cout &lt;&lt; (x.x + y.y) &lt;&lt; &quot; &quot;; }, w, uu); // error: can't find ra::start(U)
</pre></div>

<span id="x_002dnew_002darray_002doperations"></span></div>
<div class="subsection" id="New-array-operations">
<h4 class="subsection">2.10.2 New array operations</h4>

<p><code>ra::</code> provides array extensions for standard operations such as <code>+</code>, <code>*</code>, <code>cos</code> <a href="#x_002dscalar_002dops">and so on</a>. You can add array extensions for your own operations in the obvious way, with <a href="#x_002dmap"><code>map</code></a> (but note the namespace qualifiers):
</p>
<div class="example">
<pre class="verbatim">return_type my_fun(...) { };
...
namespace ra {
template &lt;class ... A&gt; inline auto
my_fun(A &amp;&amp; ... a)
{
    return map(::my_fun, std::forward&lt;A&gt;(a) ...);
}
} // namespace ra
</pre></div>

<span id="index-overload-set"></span>
<p>If <code>my_fun</code> is an overload set, you can use<a id="DOCF10" href="#FOOT10"><sup>10</sup></a>
</p>
<div class="example">
<pre class="verbatim">namespace ra {
template &lt;class ... A&gt; inline auto
my_fun(A &amp;&amp; ... a)
{
    return map([](auto &amp;&amp; ... a) { return ::my_fun(a ...); }, std::forward&lt;A&gt;(a) ...);
}
} // namespace ra
</pre></div>

<span id="index-error"></span>
<span id="index-assert"></span>
<hr>
</div>
</div>
<div class="section" id="Error-handling">
<div class="header">
<p>
Previous: <a href="#Extension" accesskey="p" rel="prev">Extension</a>, Up: <a href="#Usage" accesskey="u" rel="up">Usage</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Error-handling-1"></span><h3 class="section">2.11 Error handling</h3>

<p>Error handling in <code>ra::</code> is controlled by two macros:
</p>
<ul>
<li> <code>RA_DO_CHECK</code>
is a binary flag that controls runtime checks. The default is 1 which means to check for errors. 0 means not to check. The checks themselves are done with <code>RA_ASSERT</code>.
</li><li> <code>RA_ASSERT(cond, ...)</code>
is a function-like macro. <code>cond</code> is an expression that evaluates to true (in the <code>ra::</code> namespace) if the assertion is satisfied. The other arguments are informative and do not need to be used. If the assertion fails, the default definition of <code>RA_ASSERT(cond, ...)</code> prints those arguments to <code>std::cerr</code> and aborts.
</li></ul>

<p><code>ra::</code> contains uses of <code>assert</code> for checking invariants or for sanity checks that are separate from uses of <code>RA_ASSERT</code>. Those can be disabled in the usual way with <samp>-DNDEBUG</samp>.
</p>
<p>You can redefine <code>RA_ASSERT</code> to something that is more appropriate for your program. <code>examples/throw.cc</code> in the distribution shows how to throw a user-defined exception instead.
</p>
<hr>
</div>
</div>
<div class="chapter" id="Extras">
<div class="header">
<p>
Next: <a href="#Hazards" accesskey="n" rel="next">Hazards</a>, Previous: <a href="#Usage" accesskey="p" rel="prev">Usage</a>, Up: <a href="#Top" accesskey="u" rel="up"><code>ra::</code></a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Extras-1"></span><h2 class="chapter">3 Extras</h2>


<hr>
</div>
<div class="chapter" id="Hazards">
<div class="header">
<p>
Next: <a href="#Internals" accesskey="n" rel="next">Internals</a>, Previous: <a href="#Extras" accesskey="p" rel="prev">Extras</a>, Up: <a href="#Top" accesskey="u" rel="up"><code>ra::</code></a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Hazards-1"></span><h2 class="chapter">4 Hazards</h2>

<p>Some of these issues arise because <code>ra::</code> applies its principles systematically, which can have surprising results. Still others are the result of unfortunate compromises. And a few are just bugs.
</p>
<ul class="section-toc">
<li><a href="#Reuse-of-expression-objects" accesskey="1">Reuse of expression objects</a></li>
<li><a href="#Assignment-to-views" accesskey="2">Assignment to views</a></li>
<li><a href="#View-of-const-vs-const-view" accesskey="3">View of const vs const view</a></li>
<li><a href="#Rank-extension-in-assignments" accesskey="4">Rank extension in assignments</a></li>
<li><a href="#Performance-pitfalls-of-rank-extension" accesskey="5">Performance pitfalls of rank extension</a></li>
<li><a href="#Chained-assignment" accesskey="6">Chained assignment</a></li>
<li><a href="#Unregistered-scalar-types" accesskey="7">Unregistered scalar types</a></li>
</ul>
<div class="section" id="Reuse-of-expression-objects">
<h3 class="section">4.1 Reuse of expression objects</h3>

<p>Expression objects are meant to be used once. This applies to anything produced with <code>ra::map</code>, <code>ra::iter</code>, <code>ra::start</code>, or <code>ra::ptr</code>. Reuse errors are <em>not</em> checked. For example:
</p>
<div class="example">
<pre class="verbatim">ra::Big&lt;int, 2&gt; B({3, 3}, ra::_1 + ra::_0*3); // {{0 1 2} {3 4 5} {6 7 8}}
std::array&lt;int, 2&gt; l = { 1, 2 };
cout &lt;&lt; B(ra::ptr(l), ra::ptr(l)) &lt;&lt; endl; // ok =&gt; {{4 5} {7 8}}
auto ll = ra::ptr(l);
cout &lt;&lt; B(ll, ll) &lt;&lt; endl; // ??
</pre></div>

</div>
<div class="section" id="Assignment-to-views">
<h3 class="section">4.2 Assignment to views</h3>

<p>FIXME
With rt-shape containers (e.g. <code>Big</code>), <code>operator=</code> replaces the left hand side instead of writing over its contents. This behavior is inconsistent with <code>View::operator=</code> and is there only so that istream <code>&gt;&gt;</code> container may work; do not rely on it.
</p>
</div>
<div class="section" id="View-of-const-vs-const-view">
<h3 class="section">4.3 View of const vs const view</h3>


<p>FIXME
Passing view arguments by reference
</p>
</div>
<div class="section" id="Rank-extension-in-assignments">
<h3 class="section">4.4 Rank extension in assignments</h3>

<p>Assignment of an expression onto another expression of lower rank may not do what you expect. This example matches <code>a</code> and 3 [both of shape ()] with a vector of shape (3). This is equivalent to <code>{a=3+4; a=3+5; a=3+6;}</code>. You may get a different result depending on traversal order.
</p>
<div class="example">
<pre class="verbatim">int a = 0;
ra::scalar(a) = 3 + ra::Small&lt;int, 3&gt; {4, 5, 6}; // ?
</pre><pre class="example">  &rArr; a = 9
</pre></div>

<p>Compare with
</p>
<div class="example">
<pre class="verbatim">int a = 0;
ra::scalar(a) += 3 + ra::Small&lt;int, 3&gt; {4, 5, 6}; // 0 + 3 + 4 + 5 + 6
</pre><pre class="example">  &rArr; a = 18
</pre></div>

</div>
<div class="section" id="Performance-pitfalls-of-rank-extension">
<h3 class="section">4.5 Performance pitfalls of rank extension</h3>

<p>In the following example where <code>b</code> has its shape extended from (3) to (3, 4), <code>f</code> is called 12 times, even though only 3 calls are needed if <code>f</code> doesn&rsquo;t have side effects. In such cases it might be preferrable to write the outer loop explicitly, or to do some precomputation.
</p>
<div class="example">
<pre class="verbatim">ra::Big&lt;int, 2&gt; a = {{1, 2, 3, 4}, {5, 6, 7, 8} {9, 10, 11, 12}};
ra::Big&lt;int, 1&gt; b = {1, 2, 3};
ra::Big&lt;int, 2&gt; c = map(f, b) + a;
</pre></div>

</div>
<div class="section" id="Chained-assignment">
<h3 class="section">4.6 Chained assignment</h3>

<p>FIXME
When <code>a=b=c</code> works, it operates as <code>b=c; a=b;</code> and not as an array expression.
</p>
</div>
<div class="section" id="Unregistered-scalar-types">
<h3 class="section">4.7 Unregistered scalar types</h3>

<p>FIXME
<code>View&lt;T, N&gt; x; x = T()</code> fails if <code>T</code> isn&rsquo;t registered as <code>is_scalar</code>.
</p>
<ol>
<li> Item 0
</li><li> Item 1
</li><li> Item 2
</li></ol>

<hr>
</div>
</div>
<div class="chapter" id="Internals">
<div class="header">
<p>
Next: <a href="#The-future" accesskey="n" rel="next">The future</a>, Previous: <a href="#Hazards" accesskey="p" rel="prev">Hazards</a>, Up: <a href="#Top" accesskey="u" rel="up"><code>ra::</code></a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Internals-1"></span><h2 class="chapter">5 Internals</h2>

<p><code>ra::</code> has two main components: a set of container classes, and the expression template mechanism. The container classes provide leaves for the expression template trees, and the container classes also make some use of the expression template mechanism internally (e.g. in the selection operator, or for initialization).
</p>

<ul class="section-toc">
<li><a href="#Headers" accesskey="1">Headers</a></li>
<li><a href="#Type-hierarchy" accesskey="2">Type hierarchy</a></li>
<li><a href="#Term-agreement" accesskey="3">Term agreement</a></li>
<li><a href="#Loop-types" accesskey="4">Loop types</a></li>
<li><a href="#Introspection" accesskey="5">Introspection</a></li>
<li><a href="#Building-with-ra_003a_003a" accesskey="6">Building with <code>ra::</code></a></li>
</ul>
<hr>
<div class="section" id="Headers">
<div class="header">
<p>
Next: <a href="#Type-hierarchy" accesskey="n" rel="next">Type hierarchy</a>, Up: <a href="#Internals" accesskey="u" rel="up">Internals</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Headers-1"></span><h3 class="section">5.1 Headers</h3>

<p>The header structure of <code>ra::</code> is as follows.<a id="DOCF11" href="#FOOT11"><sup>11</sup></a>
</p>
<ul>
<li> <code>tuples.hh</code> -
Generic macros and tuple library.
</li><li> <code>bootstrap.hh</code> -
Basic types, concepts, type predicates, introspection.
</li><li> <code>expr.hh</code> -
Expression template nodes, prefix matching.
</li><li> <code>ply.hh</code> -
Traversal, including I/O.
</li><li> <code>small.hh</code> -
Array type with compile time dimensions.
</li><li> <code>big.hh</code> -
Array type with run time dimensions.
</li><li> <code>optimize.hh</code> -
Optimization templates.
</li><li> <code>ra.hh</code> -
Functions and operators. Main header.
</li><li> <code>test.hh</code> -
(accessory) Testing library.
</li><li> <code>bench.hh</code> -
(accessory) Benchmarking library.
</li><li> <code>dual.hh</code> -
(accessory) Dual number type and operations.
</li></ul>

<img src="headers.png" alt="headers">

<hr>
</div>
<div class="section" id="Type-hierarchy">
<div class="header">
<p>
Next: <a href="#Term-agreement" accesskey="n" rel="next">Term agreement</a>, Previous: <a href="#Headers" accesskey="p" rel="prev">Headers</a>, Up: <a href="#Internals" accesskey="u" rel="up">Internals</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Type-hierarchy-1"></span><h3 class="section">5.2 Type hierarchy</h3>

<p>Some of the categories below are C++20 ‘concepts’, some are still informal.
</p>
<ul>
<li> <b>Container</b> &mdash; <code>Big</code>, <code>Shared</code>, <code>Unique</code>, <code>Small</code>

<p>These are array types that own their data in one way or another. Creating or destroying these objects may allocate or deallocate memory, respectively.
</p>
</li><li> <b>View</b> &mdash; <code>View</code>, <code>SmallView</code>

<p>These are array views into data in memory, which may be writable. Any of the <b>Container</b> types can be treated as a <b>View</b>, but one may also create <b>View</b>s that aren&rsquo;t associated with any <b>Container</b>, for example into memory allocated by a different library. Creating and destroying <b>View</b>s doesn&rsquo;t allocate or deallocate memory for array elements.
</p>
</li><li> <b>Iterator</b> &mdash; <code>CellBig</code>, <code>CellSmall</code>, <code>Iota</code>, <code>Ptr</code>, <code>Scalar</code>, <code>Expr</code>, <code>Pick</code>

<p>This is a traversable object. <b>Iterator</b>s are accepted by all the array functions such as <code>map</code>, <code>for_each</code>, etc. <code>map</code> produces an <b>Iterator</b> itself, so most array expressions are <b>Iterator</b>s. <b>Iterator</b>s are created from <b>View</b>s and from certain foreign array-like types primarily through the function <code>start</code>. This is done automatically when those types are used in array expressions.
</p>
<p><b>Iterator</b>s have two traversal functions: <code>.adv(k, d)</code>, moves the iterator along any dimension <var>k</var>, and <code>.mov(d)</code>, is used on linearized views of the array. The methods <code>len()</code>, <code>step()</code>, <code>keep_step()</code> are used to determine the extent of these linearized views. In this way, a loop involving <b>Iterator</b>s can have its inner loop unfolded, which is faster than a multidimensional loop, especially if the inner dimensions of the loop are small.
</p>
<p><b>Iterator</b>s also provide an <code>at(i ...)</code> method for random access to any element.
</p>
</li></ul>


<hr>
</div>
<div class="section" id="Term-agreement">
<div class="header">
<p>
Next: <a href="#Loop-types" accesskey="n" rel="next">Loop types</a>, Previous: <a href="#Type-hierarchy" accesskey="p" rel="prev">Type hierarchy</a>, Up: <a href="#Internals" accesskey="u" rel="up">Internals</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Term-agreement-1"></span><h3 class="section">5.3 Term agreement</h3>

<p>The execution of an expression template begins with the determination of its shape — the length of each of its dimensions. This is done recursively by traversing the terms of the expression. For a given dimension <code>k</code>≥0, terms that have rank less or equal than <code>k</code> are ignored, following the prefix matching principle. Likewise terms where dimension <code>k</code> has undefined length (such as <code>iota()</code> or dimensions created with <code>insert</code>) are ignored. All the other terms must match.
</p>
<p>Then we select a order of traversal. <code>ra::</code> supports ‘array’ orders, meaning that the dimensions are sorted in a certain way from outermost to innermost and a full dimension is traversed before one advances on the dimension outside. However, currently (v26) there is no heuristic to choose a dimension order, so traversal always happens in row-major order (which shouldn&rsquo;t be relied upon). <code>ply_ravel</code> will unroll as many innermost dimensions as it can, and in some cases traversal will be executed as a flat loop.
</p>
<p>Finally we select a traversal method. <code>ra::</code> has two traversal methods: <code>ply_fixed</code> can be used when the rank and the traversal order are known at compile time, and <code>ply_ravel</code> can be used in the general case.
</p>
<hr>
</div>
<div class="section" id="Loop-types">
<div class="header">
<p>
Next: <a href="#Introspection" accesskey="n" rel="next">Introspection</a>, Previous: <a href="#Term-agreement" accesskey="p" rel="prev">Term agreement</a>, Up: <a href="#Internals" accesskey="u" rel="up">Internals</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Loop-types-1"></span><h3 class="section">5.4 Loop types</h3>

<p>TODO
</p>
<hr>
</div>
<div class="section" id="Introspection">
<div class="header">
<p>
Next: <a href="#Building-with-ra_003a_003a" accesskey="n" rel="next">Building with <code>ra::</code></a>, Previous: <a href="#Loop-types" accesskey="p" rel="prev">Loop types</a>, Up: <a href="#Internals" accesskey="u" rel="up">Internals</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Introspection-1"></span><h3 class="section">5.5 Introspection</h3>

<p>The following functions are available to query the properties of <code>ra::</code> objects.
</p>
<span id="index-rank-1"></span>
<span id="x_002drank"></span><dl class="def">
<dt id="index-rank-2"><span class="category">Function<!-- /@w -->: </span><span><em>rank_t</em> <strong>rank</strong> <em>e</em><a href='#index-rank-2' class='copiable-anchor'> &para;</a></span></dt>
<dd>
<p>Return the rank of expression <var>e</var>.
</p>
</dd></dl>

<span id="index-shape-1"></span>
<span id="x_002dshape"></span><dl class="def">
<dt id="index-shape-2"><span class="category">Function<!-- /@w -->: </span><span><em>array</em> <strong>shape</strong> <em>e</em><a href='#index-shape-2' class='copiable-anchor'> &para;</a></span></dt>
<dt id="index-shape-3"><span class="category">Function<!-- /@w -->: </span><span><em>dim_t</em> <strong>shape</strong> <em>e k</em><a href='#index-shape-3' class='copiable-anchor'> &para;</a></span></dt>
<dd>
<p>The first form returns the shape of expression <var>e</var> as an array. The second form returns the length of axis <var>k</var>, i.e. <code>shape(e)[k]</code> ≡ <code>shape(e, k)</code>.
</p>
<p><var>array</var> might be a <a href="#x_002dforeign_002dtype"><code>foreign type</code></a> (such as <code>std::array</code> or <code>std::vector</code>) instead of a <code>ra-ra</code> type.
</p>
</dd></dl>


<hr>
</div>
<div class="section" id="Building-with-ra_003a_003a">
<div class="header">
<p>
Previous: <a href="#Introspection" accesskey="p" rel="prev">Introspection</a>, Up: <a href="#Internals" accesskey="u" rel="up">Internals</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Building-with-ra_003a_003a-1"></span><h3 class="section">5.6 Building with <code>ra::</code></h3>

<p>The following <code>#define</code>s affect the behavior of <code>ra::</code>.
</p>
<ul>
<li> <code>RA_DO_CHECK</code> (default 1):  Check shape agreement (e.g. <code>Big&lt;int, 1&gt; {2, 3} + Big&lt;int, 1&gt; {1, 2, 3}</code>) and random array accesses (e.g. <code>Small&lt;int, 2&gt; a = 0; int i = 10; a[i] = 0;</code>). See <a href="#Error-handling">Error handling</a>.
</li><li> <code>RA_DO_OPT</code> (default 1): Sets the default for all <code>RA_DO_OPT_XXX</code> flags.
</li><li> <code>RA_DO_OPT_IOTA</code> (default 1): Perform immediately (beat) certain operations on <code>ra::Iota</code> objects. For example, <code>ra::iota(3, 0) + 1</code> becomes <code>ra::iota(3, 1)</code> instead of a two-operand expression template.
</li><li> <code>RA_DO_OPT_SMALLVECTOR</code> (default 0): Perform immediately certain operations on <code>ra::Small</code> objects, using small vector intrinsics. Currently this only works on <b>gcc</b> and doesn&rsquo;t necessarily result in improved performance.
</li></ul>

<p><code>ra::</code> comes with three kinds of tests: examples, proper tests, and benchmarks. <code>ra::</code> uses its own test and benchmark suites. Run <code>CXXFLAGS=-O3 scons</code> from the top directory of the distribution to build and run them all.  Alternatively, you can use <code>CXXFLAGS=-O3 cmake . &amp;&amp; make &amp;&amp; make test</code>. <code>ra::</code> is highly dependent on optimization by the compiler and will be much slower with <samp>-O0</samp>.
</p>
<p>The following environment variables affect the test suite under SCons:
</p>
<ul>
<li> <code>RA_USE_BLAS</code> (default 0): Use BLAS for <code>gemm</code> and <code>gemv</code> benchmarks.
</li></ul>


<hr>
</div>
</div>
<div class="chapter" id="The-future">
<div class="header">
<p>
Next: <a href="#Reference" accesskey="n" rel="next">Reference</a>, Previous: <a href="#Internals" accesskey="p" rel="prev">Internals</a>, Up: <a href="#Top" accesskey="u" rel="up"><code>ra::</code></a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
</div>
<span id="The-future-1"></span><h2 class="chapter">6 The future</h2>

<ul class="section-toc">
<li><a href="#Error-messages" accesskey="1">Error messages</a></li>
<li><a href="#Reductions" accesskey="2">Reductions</a></li>
<li><a href="#Etc" accesskey="3">Etc</a></li>
</ul>
<div class="section" id="Error-messages">
<h3 class="section">6.1 Error messages</h3>

<p>FIXME
</p>
</div>
<div class="section" id="Reductions">
<h3 class="section">6.2 Reductions</h3>

<p>FIXME
</p>
</div>
<div class="section" id="Etc">
<h3 class="section">6.3 Etc</h3>

<p>FIXME
</p>
<hr>
</div>
</div>
<div class="chapter" id="Reference">
<div class="header">
<p>
Next: <a href="#Sources" accesskey="n" rel="next">Sources</a>, Previous: <a href="#The-future" accesskey="p" rel="prev">The future</a>, Up: <a href="#Top" accesskey="u" rel="up"><code>ra::</code></a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Reference-1"></span><h2 class="chapter">7 Reference</h2>

<span id="index-agree"></span>
<span id="x_002dagree"></span><dl class="def">
<dt id="index-agree-1"><span class="category">Function: </span><span><strong>agree</strong> <em>arg ...</em><a href='#index-agree-1' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Return true if the shapes of arguments <var>arg...</var> match (see <a href="#Rank-extension">Rank extension</a>), else return false.
</p>
<p>This is useful when <a href="#Error-handling">error checking</a> is enabled and one wants to avoid the failure response.
</p><div class="example">
<pre class="verbatim">    ra::Small&lt;int, 2, 3&gt; A;
    ra::Small&lt;int, 2&gt; B;
    ra::Small&lt;int, 3&gt; C;

    agree(A, B); // -&gt; true
    static_assert(agree(A, B)); // ok for ct shapes
    cout &lt;&lt; (A+B) &lt;&lt; endl; // ok

    agree(A, C); // -&gt; false
    cout &lt;&lt; (A+C) &lt;&lt; endl; // error. Maybe abort, maybe throw - cf Error Handling
</pre></div>
</dd></dl>

<span id="index-agree_005fop"></span>
<span id="x_002dagree_005fop"></span><dl class="def">
<dt id="index-agree_005fop-1"><span class="category">Function: </span><span><strong>agree_op</strong> <em>op arg ...</em><a href='#index-agree_005fop-1' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Return true if the shapes of arguments <var>arg...</var> match (see <a href="#Rank-extension">Rank extension</a>) relative to operator <var>op</var>, else return false.
</p>
<p>This differs from <a href="#x_002dagree"><code>agree</code></a> when <var>op</var> has non-zero argument ranks. For example:
</p><div class="example">
<pre class="verbatim">    ra::Big&lt;real, 1&gt; a({3}, 0.);
    ra::Big&lt;real, 2&gt; b({2, 3}, 0.n);

    agree(a, b); // -&gt; false
    cout &lt;&lt; (a+b) &lt;&lt; endl; // error

    agree_op(ra::wrank&lt;1, 1&gt;(std::plus()), a, b); // -&gt; true
    cout &lt;&lt; map(ra::wrank&lt;1, 1&gt;(std::plus()), a, b) &lt;&lt; endl; // ok
</pre></div>
</dd></dl>

<span id="index-at"></span>
<span id="x_002dat"></span><dl class="def">
<dt id="index-at-1"><span class="category">Function: </span><span><strong>at</strong> <em>expr indices</em><a href='#index-at-1' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Look up <var>expr</var> at each element of <var>indices</var>, which shall be a multi-index into <var>expr</var>.
</p>
<p>This can be used for sparse subscripting. For example:
</p><div class="example">
<pre class="verbatim">    ra::Big&lt;int, 2&gt; A = {{100, 101}, {110, 111}, {120, 121}};
    ra::Big&lt;ra::Small&lt;int, 2&gt;, 2&gt; i = {{{0, 1}, {2, 0}}, {{1, 0}, {2, 1}}};
    ra::Big&lt;int, 2&gt; B = at(A, i);
</pre><pre class="example">  &rArr; B = {{101, 120}, {110, 121}}
</pre></div>

</dd></dl>

<span id="index-cast"></span>
<span id="x_002dcast"></span><dl class="def">
<dt id="index-cast-1"><span class="category">Function: </span><span><strong>cast</strong> <em>&lt;type&gt; expr</em><a href='#index-cast-1' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Create an array expression that casts <var>expr</var> into <var>type</var>.
</p></dd></dl>

<span id="index-collapse"></span>
<span id="x_002dcollapse"></span><dl class="def">
<dt id="index-collapse-1"><span class="category">Function: </span><span><strong>collapse</strong><a href='#index-collapse-1' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>TODO
See also <a href="#x_002dexplode"><code>explode</code></a>.
</p></dd></dl>

<span id="index-concrete"></span>
<span id="x_002dconcrete"></span><dl class="def">
<dt id="index-concrete-1"><span class="category">Function: </span><span><strong>concrete</strong> <em>a</em><a href='#index-concrete-1' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Convert the argument to a container of the same shape as <var>a</var>.
</p>
<p>If the argument has rt or ct shape, it is the same for the result. The main use of this function is to obtain modifiable copy of an array expression without having to prepare a container beforehand, or compute the appropiate type.
</p>

</dd></dl>

<span id="index-diag"></span>
<span id="x_002ddiag"></span><dl class="def">
<dt id="index-diag-1"><span class="category">Function: </span><span><strong>diag</strong> <em>view</em><a href='#index-diag-1' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Equivalent to <code>transpose&lt;0, 0&gt;(view)</code>.
</p></dd></dl>

<span id="index-explode"></span>
<span id="x_002dexplode"></span><dl class="def">
<dt id="index-explode-1"><span class="category">Function: </span><span><strong>explode</strong><a href='#index-explode-1' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>TODO
</p>
<p>See also <a href="#x_002dcollapse"><code>collapse</code></a>.
</p></dd></dl>

<span id="index-for_005feach"></span>
<span id="x_002dfor_005feach"></span><dl class="def">
<dt id="index-for_005feach-1"><span class="category">Function: </span><span><strong>for_each</strong> <em>op expr ...</em><a href='#index-for_005feach-1' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Create an array expression that applies <var>op</var> to <var>expr</var> ..., and traverse it. The return value of <var>op</var> is discarded.
</p>
<p>For example:
</p><div class="example">
<pre class="verbatim">double s = 0.;
for_each([&amp;s](auto &amp;&amp; a) { s+=a; }, ra::Small&lt;double, 1&gt; {1., 2., 3})
</pre><pre class="example">&rArr; s = 6.
</pre></div>

<p>See also <a href="#x_002dmap"><code>map</code></a>.
</p>
</dd></dl>

<span id="index-format_005farray"></span>
<span id="x_002dformat_005farray"></span><dl class="def">
<dt id="index-format_005farray-1"><span class="category">Function: </span><span><strong>format_array</strong> <em>expr [last_axis_separator [second_last_axis_separator ...]]</em><a href='#index-format_005farray-1' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Formats an array for character output.
</p>
<p>For example:
</p>
<div class="example">
<pre class="verbatim">ra::Small&lt;int, 2, 2&gt; A = {{1, 2}, {3, 4}};
cout &lt;&lt; &quot;case a:\n&quot; &lt;&lt; A &lt;&lt; endl;
cout &lt;&lt; &quot;case b:\n&quot; &lt;&lt; format_array(A) &lt;&lt; endl;
cout &lt;&lt; &quot;case c:\n&quot; &lt;&lt;  format_array(A, &quot;|&quot;, &quot;-&quot;) &lt;&lt; endl;
</pre><pre class="example">-| case a:
1 2
3 4
case b:
1 2
3 4
case c:
1|2-3|4
</pre></div>

<p>The shape that might be printed before the expression itself (depending on its type) is not subject to these separators.
</p>
<p>See also <a href="#x_002dnoshape"><code>noshape</code></a>, <a href="#x_002dwithshape"><code>withshape</code></a>.
</p>
</dd></dl>

<span id="index-from"></span>
<span id="x_002dfrom"></span><dl class="def">
<dt id="index-from-1"><span class="category">Function: </span><span><strong>from</strong> <em>op expr ...</em><a href='#index-from-1' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Create outer product expression. This is defined as
</p>
<div class="display">
<pre class="display">E = from(op, e₀, e₁ ...) ⇒ E(i₀₀, i₀₁ ..., i₁₀, i₁₁, ..., ...) = op[expr₀(i₀₀, i₀₁, ...), expr₁(i₁₀, i₁₁, ...), ...].
</pre></div>

<p>For example:
</p><div class="example">
<pre class="verbatim">    ra::Big&lt;double, 1&gt; a {1, 2, 3};
    ra::Big&lt;double, 1&gt; b {10, 20, 30};
    ra::Big&lt;double, 2&gt; axb = from([](auto &amp;&amp; a, auto &amp;&amp; b) { return a*b; }, a, b)
</pre><pre class="example">  &rArr; axb = {{10, 20, 30}, {20, 40, 60}, {30, 60, 90}}
</pre></div>

<div class="example">
<pre class="verbatim">    ra::Big&lt;int, 1&gt; i {2, 1};
    ra::Big&lt;int, 1&gt; j {0, 1};
    ra::Big&lt;double, 2&gt; A = {{1, 2}, {3, 4}, {5, 6}};
    ra::Big&lt;double, 2&gt; Aij = from(A, i, j)
</pre><pre class="example">  &rArr; Aij = {{6, 5}, {4, 3}}
</pre></div>

<p>The last example is more or less how <code>A(i, j)</code> is implemented for arbitrary subscripts (see <a href="#The-rank-conjunction">The rank conjunction</a>).
</p>
</dd></dl>

<span id="index-imag_005fpart"></span>
<span id="x_002dimag_005fpart"></span><dl class="def">
<dt id="index-imag_005fpart-1"><span class="category">Function: </span><span><strong>imag_part</strong><a href='#index-imag_005fpart-1' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Take imaginary part of a complex number. This can be used as reference.
</p>
<p>For example: </p>
<div class="example">
<pre class="verbatim">ra::Small&lt;std::complex&lt;double&gt;, 2, 2&gt; A = {{1., 2.}, {3., 4.}};
imag_part(A) = -2*real_part(A);
cout &lt;&lt; A &lt;&lt; endl;
</pre><pre class="example">-|
(1, -2) (2, -4)
(3, -6) (4, -8)
</pre></div>

<p>See also <a href="#x_002dreal_005fpart"><code>real_part</code></a>.
</p></dd></dl>

<span id="index-map"></span>
<span id="x_002dmap"></span><dl class="def">
<dt id="index-map-1"><span class="category">Function: </span><span><strong>map</strong> <em>op expr ...</em><a href='#index-map-1' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Create an array expression that applies callable <var>op</var> to <var>expr</var> ...
</p>
<p>For example:
</p><div class="example">
<pre class="verbatim">ra::Big&lt;double, 1&gt; x = map(cos, std::array {0.});
</pre><pre class="example">&rArr; x = { 1. }
</pre></div>

<p><var>op</var> can return a reference. For example:
</p>
<div class="example">
<pre class="verbatim">ra::Big&lt;int, 2&gt; x = {{3, 3}, 0.};
ra::Big&lt;int, 2&gt; i = {0, 1, 1, 2};
ra::Big&lt;int, 2&gt; j = {1, 0, 2, 1};
map(x, i, j) = 1;
</pre><pre class="example">&rArr; x = {{0, 1, 0}, {1, 0, 1}, {0, 1, 0}}
</pre></div>

<p>Note that <code>map(x, i, j)</code> is <a href="#x_002dsubscript_002douter_002dproduct">not the same</a> as <code>x(i, j)</code>.
</p>
<p><var>op</var> can be any callable. For example:
</p><div class="example">
<pre class="verbatim">struct A { int a, b; };
std::vector&lt;A&gt; v = {{1, 2}, {3, 4}};
ra::map(&amp;A::a, v) = -ra::map(&amp;A::b, v); // pointer to member
</pre><pre class="example">&rArr; v = {{-2, 2}, {-4, 4}}
</pre></div>

<p>See also <a href="#x_002dfor_005feach"><code>for_each</code></a>.
</p>
</dd></dl>

<span id="index-pack"></span>
<span id="x_002dpack"></span><dl class="def">
<dt id="index-pack-1"><span class="category">Function: </span><span><strong>pack</strong> <em>&lt;type&gt; expr ...</em><a href='#index-pack-1' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Create an array expression that brace-constructs <var>type</var> from <var>expr</var> ...
</p></dd></dl>

<span id="index-pick"></span>
<span id="x_002dpick"></span><dl class="def">
<dt id="index-pick-1"><span class="category">Function: </span><span><strong>pick</strong> <em>select_expr expr ...</em><a href='#index-pick-1' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Create an array expression that selects the first of <var>expr</var> ... if <var>select_expr</var> is 0, the second if <var>select_expr</var> is 1, and so on. The expressions that are not selected are not looked up.
</p>
<p>This function cannot be defined using <a href="#x_002dmap"><code>map</code></a>, because <code>map</code> looks up each one of its argument expressions before calling <var>op</var>.
</p>
<p>For example:
</p><div class="example">
<pre class="verbatim">    ra::Small&lt;int, 3&gt; s {2, 1, 0};
    ra::Small&lt;double, 3&gt; z = pick(s, s*s, s+s, sqrt(s));
</pre><pre class="example">  &rArr; z = {1.41421, 2, 0}
</pre></div>

</dd></dl>

<span id="index-ply"></span>
<span id="x_002dply"></span><dl class="def">
<dt id="index-ply-1"><span class="category">Function: </span><span><strong>ply</strong> <em>expr</em><a href='#index-ply-1' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Traverse <var>expr</var>. <code>ply</code> returns <code>void</code> so <var>expr</var> should be run for effect.
</p>
<p>It is rarely necessary to use <code>ply</code>. Expressions are traversed automatically when they are assigned to views, for example, or printed out. <a href="#x_002dfor_005feach"><code>for_each</code></a><code>(...)</code>, which is equivalent to <code>ply(map(...))</code>, should cover most other uses.
</p>
<div class="example">
<pre class="verbatim">double s = 0.;
ply(map([&amp;s](auto &amp;&amp; a) { s+=a; }, ra::Small&lt;double, 1&gt; {1., 2., 3})) // same as for_each
</pre><pre class="example">&rArr; s = 6.
</pre></div>

</dd></dl>

<span id="index-real_005fpart"></span>
<span id="x_002dreal_005fpart"></span><dl class="def">
<dt id="index-real_005fpart-1"><span class="category">Function: </span><span><strong>real_part</strong><a href='#index-real_005fpart-1' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Take real part of a complex number. This can be used as reference.
</p>
<p>See also <a href="#x_002dimag_005fpart"><code>imag_part</code></a>.
</p></dd></dl>

<span id="index-reverse"></span>
<span id="x_002dreverse"></span><dl class="def">
<dt id="index-reverse-1"><span class="category">Function: </span><span><strong>reverse</strong> <em>view axis</em><a href='#index-reverse-1' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Create a new view by reversing axis <var>k</var> of <var>view</var>.
</p>
<p>This is equivalent to <code>view(ra::dots&lt;k&gt;, ra::iota(ra::len, ra::len-1, -1))</code>.
</p>
<p>This operation does not work on arbitrary array expressions yet. TODO FILL
</p>
</dd></dl>

<span id="index-size"></span>
<span id="x_002dsize"></span><dl class="def">
<dt id="index-size-1"><span class="category">Function: </span><span><strong>size</strong> <em>a</em><a href='#index-size-1' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Get the total size of an <code>ra::</code> object: the product of all its lengths.
</p></dd></dl>


<span id="index-stencil"></span>
<span id="x_002dstencil"></span><dl class="def">
<dt id="index-stencil-1"><span class="category">Function: </span><span><strong>stencil</strong> <em>view lo hi</em><a href='#index-stencil-1' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Create a stencil on <var>view</var> with lower bounds <var>lo</var> and higher bounds <var>hi</var>.
</p>
<p><var>lo</var> and <var>hi</var> are expressions of rank 1 indicating the extent of the stencil on each dimension. Scalars are rank extended, that is, <var>lo</var>=0 is equivalent to <var>lo</var>=(0, 0, ..., 0) with length equal to the rank <code>r</code> of <var>view</var>. The stencil view has twice as many axes as <var>view</var>. The first <code>r</code> dimensions are the same as those of <var>view</var> except that they have their lengths reduced by <var>lo</var>+<var>hi</var>. The last <code>r</code> dimensions correspond to the stencil around each element of <var>view</var>; the center element is at <code>s(i0, i1, ..., lo(0), lo(1), ...)</code>.
</p>
<p>This operation does not work on arbitrary array expressions yet. TODO FILL
</p>
</dd></dl>

<span id="index-swap"></span>
<span id="x_002dswap"></span><dl class="def">
<dt id="index-swap-1"><span class="category">Function: </span><span><strong>swap</strong> <em>a b</em><a href='#index-swap-1' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Swap the contents of containers <var>a</var> and <var>b</var>.
</p>
<p>Both containers must be of the same storage type. The containers may have different shapes, but if at least one of them is of ct rank, then both of them must have the same rank.
</p>
<p>This function reuses <code>std::swap</code> for same-rank overloads, so it must not be qualified (i.e. use <code>swap(a, b)</code>, not <code>ra::swap(a, b)</code>).
</p></dd></dl>

<div class="example">
<pre class="verbatim">    ra::Big&lt;int&gt; a ({2, 3}, 1 + ra::_0 - ra::_1); // (1)
    ra::Big&lt;int&gt; b ({2, 3, 4}, 1 - ra::_0 + ra::_1 + ra::_2); // (2)
    swap(a, b);
    // as if (1) had b and (2) had a
</pre></div>

<span id="index-transpose"></span>
<span id="x_002dtranspose"></span><dl class="def">
<dt id="index-transpose-1"><span class="category">Function: </span><span><strong>transpose</strong> <em>&lt;axes ...&gt; (view) | (axes, view)</em><a href='#index-transpose-1' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Create a new view by transposing the axes of <var>view</var>. The number of <var>axes</var> must match the rank of <var>view</var>.
</p>
<p>For example:
</p>
<div class="example">
<pre class="verbatim">    ra::Unique&lt;double, 2&gt; a = {{1, 2, 3}, {4, 5, 6}};
    cout &lt;&lt; transpose&lt;1, 0&gt;(a) &lt;&lt; endl;
</pre><pre class="example">  -|
  3 2
  1 4
  2 5
  3 6
</pre></div>

<p>The rank of the result is <code>maxᵢ(axesᵢ)+1</code> and it may be smaller or larger than that of <var>view</var>. If an axis is repeated, the smallest of the dimensions of <var>view</var> is used. For example:
</p>
<div class="example">
<pre class="verbatim">    ra::Unique&lt;double, 2&gt; a = {{1, 2, 3}, {4, 5, 6}};
    cout &lt;&lt; transpose&lt;0, 0&gt;(a) &lt;&lt; endl; // { a(0, 0), a(1, 1) }
</pre><pre class="example">  -|
  2
  1 5
</pre></div>

<p>If one of the destination axes isn&rsquo;t mentioned in <var>axes</var>, then it becomes a ‘dead’ axis similar to those produced by <a href="#x_002dinsert"><code>insert</code></a>. For example:
</p>
<div class="example">
<pre class="verbatim">    ra::Unique&lt;double, 1&gt; a = {1, 2, 3};
    cout &lt;&lt; ((a*10) + transpose&lt;1&gt;(a)) &lt;&lt; endl;
</pre><pre class="example">  -|
  3 3
  11 21 31
  12 22 32
  13 23 33
</pre></div>

<p>The two argument form lets you specify the axis list at runtime. In that case the result will have rt rank as well. For example: </p>
<div class="example">
<pre class="verbatim">    ra::Small&lt;int, 2&gt; axes = {0, 1};
    ra::Unique&lt;double, 2&gt; a = {{1, 2, 3}, {4, 5, 6}};
    cout &lt;&lt; &quot;A: &quot; &lt;&lt; transpose(axes, a) &lt;&lt; endl;
    axes = {1, 0};
    cout &lt;&lt; &quot;B: &quot; &lt;&lt; transpose(axes, a) &lt;&lt; endl;
</pre><pre class="example">  -|
  A: 2
  2 3
  1 2 3
  4 5 6
  B: 2
  3 2
  1 4
  2 5
  3 6
</pre></div>

<p>This operation does not work on arbitrary array expressions yet. TODO FILL
</p>
</dd></dl>

<span id="index-where"></span>
<span id="x_002dwhere"></span><dl class="def">
<dt id="index-where-1"><span class="category">Function: </span><span><strong>where</strong> <em>pred_expr true_expr false_expr</em><a href='#index-where-1' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Create an array expression that selects <var>true_expr</var> if <var>pred_expr</var> is <code>true</code>, and <var>false_expr</var> if <var>pred_expr</var> is <code>false</code>. The expression that is not selected is not looked up.
</p>
<p>For example:
</p><div class="example">
<pre class="verbatim">    ra::Big&lt;double, 1&gt; s {1, -1, 3, 2};
    s = where(s&gt;=2, 2, s); // saturate s
</pre><pre class="example">  &rArr; s = {1, -1, 2, 2}
</pre></div>

</dd></dl>

<span id="index-wrank"></span>
<span id="x_002dwrank"></span><dl class="def">
<dt id="index-wrank-1"><span class="category">Function: </span><span><strong>wrank</strong> <em>&lt;input_rank ...&gt; op</em><a href='#index-wrank-1' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Wrap <var>op</var> using a rank conjunction (see <a href="#The-rank-conjunction">The rank conjunction</a>).
</p>
<p>For example: TODO
</p><div class="example">
<pre class="verbatim"></pre><pre class="example">  &rArr; x = 0
</pre></div>

</dd></dl>




<span id="index-noshape"></span>
<span id="index-withshape"></span>
<span id="x_002dnoshape"></span><span id="x_002dwithshape"></span><dl class="def">
<dt id="index-withshape-noshape"><span class="category">Special&nbsp;object<!-- /@w -->: </span><span><strong>withshape noshape</strong><a href='#index-withshape-noshape' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>If either of these objects is sent to <code>std::ostream</code> before an expression object, the shape of that object will or will not be printed.
</p>
<p>If the object has ct shape, the default is not to print the shape, so <code>noshape</code> isn&rsquo;t necessary, and conversely for <code>withshape</code> if the object has rt shape. Note that the array readers [<code>operator&gt;&gt;(std::istream &amp;, ...)</code>] expect the shape to be present or not according to the default.
</p>
<p>For example:
</p>
<div class="example">
<pre class="verbatim">ra::Small&lt;int, 2, 2&gt; A = {77, 99};
cout &lt;&lt; &quot;case a:\n&quot; &lt;&lt; A &lt;&lt; endl;
cout &lt;&lt; &quot;case b:\n&quot; &lt;&lt; ra::noshape &lt;&lt; A &lt;&lt; endl;
cout &lt;&lt; &quot;case c:\n&quot; &lt;&lt; ra::withshape &lt;&lt; A &lt;&lt; endl;
</pre><pre class="example">-| case a:
77 99
case b:
77 99
case c:
2
77 99
</pre></div>

<p>but:
</p>
<div class="example">
<pre class="verbatim">ra::Big&lt;int&gt; A = {77, 99};
cout &lt;&lt; &quot;case a:\n&quot; &lt;&lt; A &lt;&lt; endl;
cout &lt;&lt; &quot;case b:\n&quot; &lt;&lt; ra::noshape &lt;&lt; A &lt;&lt; endl;
cout &lt;&lt; &quot;case c:\n&quot; &lt;&lt; ra::withshape &lt;&lt; A &lt;&lt; endl;
</pre><pre class="example">-| case a:
1
2
77 99
case b:
77 99
case c:
1
2
77 99
</pre></div>

<p>Note that in the last example the very shape of <code>ra::Big&lt;int&gt;</code> has rt length, so that length (the rank of <code>A</code>, that is 1) is printed as part of the shape of <code>A</code>.
</p>
<p>See also <a href="#x_002dformat_005farray"><code>format_array</code></a>.
</p>
</dd></dl>

<span id="index-ptr"></span>
<span id="x_002dptr"></span><dl class="def">
<dt id="index-ptr-1"><span class="category">Function<!-- /@w -->: </span><span><strong>ptr</strong> <em>bidirectional_iterator [len]</em><a href='#index-ptr-1' class='copiable-anchor'> &para;</a></span></dt>
<dt id="index-ptr-2"><span class="category">Function<!-- /@w -->: </span><span><strong>ptr</strong> <em>bidirectional_range</em><a href='#index-ptr-2' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Create rank-1 expression from foreign object.
</p>
<p>If <code>len</code> is not given for <var>bidirectional_iterator</var>, the expression has undefined length, and it will need to be matched with other expressions whose length is defined. <code>ra::</code> doesn&rsquo;t know what is actually accessible through the iterator, so be careful. For instance:
</p>
<div class="example">
<pre class="verbatim">int pp[] = {1, 2, 3};
int * p = pp; // erase length
ra::Big&lt;int, 1&gt; v3 {1, 2, 3};
ra::Big&lt;int, 1&gt; v4 {1, 2, 3, 4};
v3 += ra::ptr(p); // ok, shape (3): v3 = {2, 4, 6}
v4 += ra::ptr(p); // undefined, shape (4): bad access to p[3]
// cout &lt;&lt; (ra::ptr(p)+ra::iota()) &lt;&lt; endl; // ct error, expression has undefined shape
cout &lt;&lt; (ra::ptr(p, 3)+ra::iota()) &lt;&lt; endl; // ok, prints { 1, 3, 5 }
cout &lt;&lt; (ra::ptr(p, 4)+ra::iota()) &lt;&lt; endl; // undefined, bad access at p[4]
</pre></div>

<p>Of course in this example one could simply have used <code>pp</code> instead of <code>ra::ptr(p)</code>, since the array type retains shape information.
</p>
<div class="example">
<pre class="verbatim">v3 += pp; // ok
v4 += pp; // error checked by ra::, shape clash (4) += (3)
cout &lt;&lt; (p + ra::iota()) &lt;&lt; endl; // ok
</pre></div>

<p>You don&rsquo;t need to use <code>ra::ptr</code> on STL containers and built in arrays, which are converted to rank-1 expressions of the right size automatically. See also <a href="#x_002dstart"><code>start</code></a>.
</p>
</dd></dl>

<span id="index-start-1"></span>
<span id="x_002dstart"></span><dl class="def">
<dt id="index-start-2"><span class="category">Function: </span><span><strong>start</strong> <em>foreign_object</em><a href='#index-start-2' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Create array expression from <var>foreign_object</var>.
</p>
<p><var>foreign_object</var> can be a built-in array (e.g. <code>int[3][2]</code>), a <code>std::random_access_range</code> type (including <code>std::vector</code> or <code>std::array</code>, see <a href="#Compatibility">Compatibility</a>), an initializer list, or any object that <code>ra::</code> accepts as scalar (see <a href="#x_002dis_002dscalar"><code>here</code></a>). The resulting expresion has shape according to the original object. Compare this with <a href="#x_002dscalar"><code>scalar</code></a>, which will always produce an expression of rank 0.
</p>
<p>Generally one can mix these types with <code>ra::</code> expressions without needing <code>ra::start</code>, but sometimes this isn&rsquo;t possible, for example for operators that must be class members.
</p>
<div class="example">
<pre class="verbatim">std::vector&lt;int&gt; x = {1, 2, 3};
ra::Big&lt;int, 1&gt; y = {10, 20, 30};
cout &lt;&lt; (x+y) &lt;&lt; endl; // same as ra::start(x)+y
// x += y; // error: no match for operator+=
ra::start(x) += y; // ok
</pre><pre class="example">-| 3
  11 22 33
  &rArr; x = { 11, 22, 33 }
</pre></div>

</dd></dl>

<span id="index-scalar"></span>
<span id="x_002dscalar"></span><dl class="def">
<dt id="index-scalar-1"><span class="category">Function: </span><span><strong>scalar</strong> <em>expr</em><a href='#index-scalar-1' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Create scalar expression from <var>expr</var>.
</p>
<p>The primary use of this function is to bring a scalar object into the <code>ra::</code> namespace. A somewhat artificial example:
</p>
<div class="example">
<pre class="verbatim">struct W { int x; }
ra::Big&lt;W, 1&gt; w { {1}, {2}, {3} };

// error: no matching function for call to start(W)
// for_each([](auto &amp;&amp; a, auto &amp;&amp; b) { cout &lt;&lt; (a.x + b.x) &lt;&lt; endl; }, w, W {7});

// bring W into ra:: with ra::scalar
for_each([](auto &amp;&amp; a, auto &amp;&amp; b) { cout &lt;&lt; (a.x + b.x) &lt;&lt; endl; }, w, ra::scalar(W {7}));
</pre><pre class="example">-| 8
   9
   10
</pre></div>

<p>See also <a href="#x_002dscalar_002dchar_002dstar"><code>this example</code></a>.
</p>
<p>Since <code>scalar</code> produces an object with rank 0, it&rsquo;s also useful when dealing with nested arrays, even for objects that are already in <code>ra::</code>. Consider:
</p><div class="example">
<pre class="verbatim">using Vec2 = ra::Small&lt;double, 2&gt;;
Vec2 x {-1, 1};
ra::Big&lt;Vec2, 1&gt; c { {1, 2}, {2, 3}, {3, 4} };
// c += x // error: x has shape (2) and c has shape (3)
c += ra::scalar(x); // ok: scalar(x) has shape () and matches c.
</pre><pre class="example">  &rArr; c = { {0, 3}, {1, 4}, {2, 5} }
</pre></div>
<p>The result is {c(0)+x, c(1)+x, c(2)+x}. Compare this with
</p><div class="example">
<pre class="verbatim">c(ra::iota(2)) += x; // c(ra::iota(2)) with shape (2) matches x with shape (2)
</pre><pre class="example">  &rArr; c = { {-1, 2}, {2, 5}, {2, 5} }
</pre></div>
<p>where the result is {c(0)+x(0), c(1)+x(1), c(2)}.
</p>
</dd></dl>

<span id="index-iter"></span>
<span id="x_002diter"></span><dl class="def">
<dt id="index-iter-1"><span class="category">Function: </span><span><strong>iter</strong> <em>&lt;k&gt; (view)</em><a href='#index-iter-1' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Create iterator over the <var>k</var>-cells of <var>view</var>. If <var>k</var> is negative, it is interpreted as the negative of the frame rank. In the current version of <code>ra::</code>, <var>view</var> may have rt or ct shape.
</p>
<div class="example">
<pre class="verbatim">ra::Big&lt;int, 2&gt; c {{1, 3, 2}, {7, 1, 3}};
cout &lt;&lt; &quot;max of each row: &quot; &lt;&lt; map([](auto &amp;&amp; a) { return amax(a); }, iter&lt;1&gt;(c)) &lt;&lt; endl;
ra::Big&lt;int, 1&gt; m({3}, 0);
scalar(m) = max(scalar(m), iter&lt;1&gt;(c));
cout &lt;&lt; &quot;max of each column: &quot; &lt;&lt; m &lt;&lt; endl;
m = 0;
for_each([&amp;m](auto &amp;&amp; a) { m = max(m, a); }, iter&lt;1&gt;(c));
cout &lt;&lt; &quot;max of each column again: &quot; &lt;&lt; m &lt;&lt; endl;
</pre><pre class="example">-| max of each row: 2
   3 7
   max of each column: 3
   7 3 3
   max of each column again: 3
   7 3 3
</pre></div>

<p>In the following example, <code>iter</code> emulates <code>scalar</code>. Note that the shape () of <code>iter&lt;1&gt;(m)</code> matches the shape (3) of <code>iter&lt;1&gt;(c)</code>. Thus, each of the 1-cells of <code>c</code> matches against the single 1-cell of <code>m</code>.
</p>
<div class="example">
<pre class="verbatim">m = 0;
iter&lt;1&gt;(m) = max(iter&lt;1&gt;(m), iter&lt;1&gt;(c));
cout &lt;&lt; &quot;max of each column yet again: &quot; &lt;&lt; m &lt;&lt; endl;
</pre><pre class="example">-| max of each column again: 3
   7 3 3
</pre></div>

<p>The following example computes the trace of each of the items [(-1)-cells] of <code>c</code>. </p>
<div class="example">
<pre class="verbatim">ra::Small&lt;int, 3, 2, 2&gt; c = ra::_0 - ra::_1 - 2*ra::_2;
cout &lt;&lt; &quot;c: &quot; &lt;&lt; c &lt;&lt; endl;
cout &lt;&lt; &quot;s: &quot; &lt;&lt; map([](auto &amp;&amp; a) { return sum(diag(a)); }, iter&lt;-1&gt;(c)) &lt;&lt; endl;
</pre><pre class="example">-| c: 0 -2
   -1 -3
   1 -1
   0 -2
   2 0
   1 -1
   s: -3 -1 -1
</pre></div>

</dd></dl>

<span id="index-sum"></span>
<span id="x_002dsum"></span><dl class="def">
<dt id="index-sum-1"><span class="category">Function: </span><span><strong>sum</strong> <em>expr</em><a href='#index-sum-1' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Return the sum (+) of the elements of <var>expr</var>, or 0 if expr is empty. This sum is performed in unspecified order.
</p></dd></dl>

<span id="index-prod"></span>
<span id="x_002dprod"></span><dl class="def">
<dt id="index-prod-1"><span class="category">Function: </span><span><strong>prod</strong> <em>expr</em><a href='#index-prod-1' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Return the product (*) of the elements of <var>expr</var>, or 1 if expr is empty. This product is performed in unspecified order.
</p></dd></dl>

<span id="index-amax"></span>
<span id="x_002damax"></span><dl class="def">
<dt id="index-amax-1"><span class="category">Function: </span><span><strong>amax</strong> <em>expr</em><a href='#index-amax-1' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Return the maximum of the elements of <var>expr</var>. If <var>expr</var> is empty, return <code>-std::numeric_limits&lt;T&gt;::infinity()</code> if the type supports it, otherwise <code>std::numeric_limits&lt;T&gt;::lowest()</code>, where <code>T</code> is the value type of the elements of <var>expr</var>.
</p></dd></dl>

<span id="index-amin"></span>
<span id="x_002damin"></span><dl class="def">
<dt id="index-amin-1"><span class="category">Function: </span><span><strong>amin</strong> <em>expr</em><a href='#index-amin-1' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Return the minimum of the elements of <var>expr</var>. If <var>expr</var> is empty, return <code>+std::numeric_limits&lt;T&gt;::infinity()</code> if the type supports it, otherwise <code>std::numeric_limits&lt;T&gt;::max()</code>, where <code>T</code> is the value type of the elements of <var>expr</var>.
</p></dd></dl>

<span id="index-early"></span>
<span id="x_002dearly"></span><dl class="def">
<dt id="index-early-1"><span class="category">Function: </span><span><strong>early</strong> <em>expr default</em><a href='#index-early-1' class='copiable-anchor'> &para;</a></span></dt>
<dd><p><var>expr</var> is an array expression that returns <code>std::optional&lt;T&gt;</code>. <var>expr</var> is traversed as by <code>for_each</code>. If the optional ever contains a value, traversal stops and that value is returned. If traversal ends, <var>default</var> is returned instead. If <code>default</code> is a reference, <code>early</code> will return its value. </p>
<p>The following definition of elementwise <code>lexicographical_compare</code> relies on <code>early</code>.
</p>
<div class="example">
<pre class="verbatim">template &lt;class A, class B&gt;
inline bool
lexicographical_compare(A &amp;&amp; a, B &amp;&amp; b)
{
    return early(map([](auto &amp;&amp; a, auto &amp;&amp; b) { return a==b ? std::nullopt : std::make_optional(a&lt;b); },
                     std::forward&lt;A&gt;(a), std::forward&lt;B&gt;(b)),
                 false);
}
</pre></div>

</dd></dl>

<span id="index-any"></span>
<span id="x_002dany"></span><dl class="def">
<dt id="index-any-1"><span class="category">Function: </span><span><strong>any</strong> <em>expr</em><a href='#index-any-1' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Return <code>true</code> if any element of <var>expr</var> is true, <code>false</code> otherwise. The traversal of the array expression will stop as soon as possible, but the traversal order is not specified.
</p></dd></dl>

<span id="index-every"></span>
<span id="x_002devery"></span><dl class="def">
<dt id="index-every-1"><span class="category">Function: </span><span><strong>every</strong> <em>expr</em><a href='#index-every-1' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Return <code>true</code> if every element of <var>expr</var> is true, <code>false</code> otherwise. The traversal of the array expression will stop as soon as possible, but the traversal order is not specified.
</p></dd></dl>

<span id="index-sqr"></span>
<span id="x_002dsqr"></span><dl class="def">
<dt id="index-sqr-1"><span class="category">Function: </span><span><strong>sqr</strong> <em>expr</em><a href='#index-sqr-1' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Compute the square of <var>expr</var>.
</p></dd></dl>

<span id="index-sqrm"></span>
<span id="x_002dsqrm"></span><dl class="def">
<dt id="index-sqrm-1"><span class="category">Function: </span><span><strong>sqrm</strong> <em>expr</em><a href='#index-sqrm-1' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Compute the square of the norm-2 of <var>expr</var>, that is, <code>conj(expr)*expr</code>.
</p></dd></dl>

<span id="index-conj"></span>
<span id="x_002dconj"></span><dl class="def">
<dt id="index-conj-1"><span class="category">Function: </span><span><strong>conj</strong> <em>expr</em><a href='#index-conj-1' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Compute the complex conjugate of <var>expr</var>.
</p></dd></dl>

<span id="index-xI"></span>
<span id="x_002dxI"></span><dl class="def">
<dt id="index-xI-1"><span class="category">Function: </span><span><strong>xI</strong> <em>expr</em><a href='#index-xI-1' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Compute <code>(0+1j)</code> times <var>expr</var>.
</p></dd></dl>

<span id="index-rel_005ferror"></span>
<span id="x_002drel_002derror"></span><dl class="def">
<dt id="index-rel_005ferror-1"><span class="category">Function: </span><span><strong>rel_error</strong> <em>a b</em><a href='#index-rel_005ferror-1' class='copiable-anchor'> &para;</a></span></dt>
<dd><p><var>a</var> and <var>b</var> are arbitrary array expressions. Compute the error of <var>a</var> relative to <var>b</var> as
</p>
<p><code>(a==0. &amp;&amp; b==0.) ? 0. : 2.*abs(a, b)/(abs(a)+abs(b))</code>
</p>
</dd></dl>

<span id="index-none-2"></span>
<span id="x_002dnone"></span><dl class="def">
<dt id="index-none-3"><span class="category">Special&nbsp;object<!-- /@w -->: </span><span><strong>none</strong><a href='#index-none-3' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Pass <code>none</code> to container constructors to indicate that the contents shouldn&rsquo;t be initialized. This is appropriate when the initialization you have in mind wouldn&rsquo;t fit in a constructor argument. For example:
</p>
<div class="example">
<pre class="verbatim">void old_style_initializer(int m, int n, double *);
ra::Big&lt;double&gt; b({2, 3}, ra::none);
old_style_initializer(2, 3, b.data());
</pre></div>

</dd></dl>

<hr>
</div>
<div class="chapter" id="Sources">
<div class="header">
<p>
Next: <a href="#Indices" accesskey="n" rel="next">Indices</a>, Previous: <a href="#Reference" accesskey="p" rel="prev">Reference</a>, Up: <a href="#Top" accesskey="u" rel="up"><code>ra::</code></a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Sources-1"></span><h2 class="chapter">8 Sources</h2>

<table>
<tr><td width="10%"></td></tr>
<tr><td width="10%"><span id="Abr70"></span>[Abr70]</td><td width="90%">Philip S. Abrams. An APL machine. Technical report SLAC-114 UC-32 (MISC), Stanford Linear Accelerator Center, Stanford University, Stanford, CA, USA, February 1970.</td></tr>
<tr><td width="10%"></td></tr>
<tr><td width="10%"><span id="Ber87"></span>[Ber87]</td><td width="90%">Robert Bernecky. An introduction to function rank. ACM SIGAPL APL Quote Quad, 18(2):39–43, December 1987.</td></tr>
<tr><td width="10%"></td></tr>
<tr><td width="10%"><span id="bli17"></span>[bli17]</td><td width="90%">The Blitz++ meta-template library. <a href="http://blitz.sourceforge.net">http://blitz.sourceforge.net</a>, November 2017.</td></tr>
<tr><td width="10%"></td></tr>
<tr><td width="10%"><span id="Cha86"></span>[Cha86]</td><td width="90%">Gregory J. Chaitin. Physics in APL2, June 1986.</td></tr>
<tr><td width="10%"></td></tr>
<tr><td width="10%"><span id="FI68"></span>[FI68]</td><td width="90%">Adin D. Falkoff and Kenneth Eugene Iverson. APL\360 User’s manual. IBM Thomas J. Watson Research Center, August 1968.</td></tr>
<tr><td width="10%"></td></tr>
<tr><td width="10%"><span id="FI73"></span>[FI73]</td><td width="90%">Adin D. Falkoff and Kenneth Eugene Iverson. The design of APL. IBM Journal of Research and Development, 17(4):5–14, July 1973.</td></tr>
<tr><td width="10%"></td></tr>
<tr><td width="10%"><span id="FI78"></span>[FI78]</td><td width="90%">Adin D. Falkoff and Kenneth Eugene Iverson. The evolution of APL. ACM SIGAPL APL, 9(1):30– 44, 1978.</td></tr>
<tr><td width="10%"></td></tr>
<tr><td width="10%"><span id="J-S"></span>[J S]</td><td width="90%">J Primer. J Software, <a href="https://www.jsoftware.com/help/primer/contents.htm">https://www.jsoftware.com/help/primer/contents.htm</a>, November 2017.</td></tr>
<tr><td width="10%"></td></tr>
<tr><td width="10%"><span id="Mat"></span>[Mat]</td><td width="90%">MathWorks. MATLAB documentation, <a href="https://www.mathworks.com/help/matlab/">https://www.mathworks.com/help/matlab/</a>, November 2017.</td></tr>
<tr><td width="10%"></td></tr>
<tr><td width="10%"><span id="num17"></span>[num17]</td><td width="90%">NumPy. <a href="http://www.numpy.org">http://www.numpy.org</a>, November 2017.</td></tr>
<tr><td width="10%"></td></tr>
<tr><td width="10%"><span id="Ric08"></span>[Ric08]</td><td width="90%">Henry Rich. J for C programmers, February 2008.</td></tr>
<tr><td width="10%"></td></tr>
<tr><td width="10%"><span id="SSM14"></span>[SSM14]</td><td width="90%">Justin Slepak, Olin Shivers, and Panagiotis Manolios. An array-oriented language with static rank polymorphism. In Z. Shao, editor, ESOP 2014, LNCS 8410, pages 27–46, 2014.</td></tr>
<tr><td width="10%"></td></tr>
<tr><td width="10%"><span id="Vel01"></span>[Vel01]</td><td width="90%">Todd Veldhuizen. Blitz++ user’s guide, February 2001.</td></tr>
<tr><td width="10%"></td></tr>
<tr><td width="10%"><span id="Wad90"></span>[Wad90]</td><td width="90%">Philip Wadler. Deforestation: transforming programs to eliminate trees. Theoretical Computer Science, 73(2): 231&ndash;248, June 1990. <a href="https://doi.org/10.1016/0304-3975%2890%2990147-A">https://doi.org/10.1016/0304-3975%2890%2990147-A</a></td></tr>
</table>

<hr>
</div>
<div class="unnumbered" id="Indices">
<div class="header">
<p>
Next: <a href="#Notes" accesskey="n" rel="next">Notes</a>, Previous: <a href="#Sources" accesskey="p" rel="prev">Sources</a>, Up: <a href="#Top" accesskey="u" rel="up"><code>ra::</code></a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Indices-1"></span><h2 class="unnumbered">Indices</h2>

<table><tr><th valign="top">Jump to: &nbsp; </th><td><a class="summary-letter" href="#Indices_cp_letter-A"><b>A</b></a>
 &nbsp; 
<a class="summary-letter" href="#Indices_cp_letter-B"><b>B</b></a>
 &nbsp; 
<a class="summary-letter" href="#Indices_cp_letter-C"><b>C</b></a>
 &nbsp; 
<a class="summary-letter" href="#Indices_cp_letter-D"><b>D</b></a>
 &nbsp; 
<a class="summary-letter" href="#Indices_cp_letter-E"><b>E</b></a>
 &nbsp; 
<a class="summary-letter" href="#Indices_cp_letter-F"><b>F</b></a>
 &nbsp; 
<a class="summary-letter" href="#Indices_cp_letter-G"><b>G</b></a>
 &nbsp; 
<a class="summary-letter" href="#Indices_cp_letter-I"><b>I</b></a>
 &nbsp; 
<a class="summary-letter" href="#Indices_cp_letter-J"><b>J</b></a>
 &nbsp; 
<a class="summary-letter" href="#Indices_cp_letter-L"><b>L</b></a>
 &nbsp; 
<a class="summary-letter" href="#Indices_cp_letter-M"><b>M</b></a>
 &nbsp; 
<a class="summary-letter" href="#Indices_cp_letter-N"><b>N</b></a>
 &nbsp; 
<a class="summary-letter" href="#Indices_cp_letter-O"><b>O</b></a>
 &nbsp; 
<a class="summary-letter" href="#Indices_cp_letter-P"><b>P</b></a>
 &nbsp; 
<a class="summary-letter" href="#Indices_cp_letter-R"><b>R</b></a>
 &nbsp; 
<a class="summary-letter" href="#Indices_cp_letter-S"><b>S</b></a>
 &nbsp; 
<a class="summary-letter" href="#Indices_cp_letter-T"><b>T</b></a>
 &nbsp; 
<a class="summary-letter" href="#Indices_cp_letter-U"><b>U</b></a>
 &nbsp; 
<a class="summary-letter" href="#Indices_cp_letter-V"><b>V</b></a>
 &nbsp; 
<a class="summary-letter" href="#Indices_cp_letter-W"><b>W</b></a>
 &nbsp; 
<a class="summary-letter" href="#Indices_cp_letter-X"><b>X</b></a>
 &nbsp; 
</td></tr></table>
<table class="index-cp" border="0">
<tr><td></td><th align="left">Index Entry</th><td>&nbsp;</td><th align="left"> Section</th></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Indices_cp_letter-A">A</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-agree"><code>agree</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-agree_005fop"><code>agree_op</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-amax"><code>amax</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-amin"><code>amin</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-any"><code>any</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-APL">APL</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Why-C_002b_002b">Why C++</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-assert">assert</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Extension">Extension</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-at"><code>at</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Indices_cp_letter-B">B</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-BLIS">BLIS</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Compatibility">Compatibility</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Blitz_002b_002b">Blitz++</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Overview">Overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Blitz_002b_002b-1">Blitz++</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-libraries">Other libraries</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-broadcasting_002c-singleton_002c-newaxis">broadcasting, singleton, newaxis</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Rank-extension">Rank extension</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-broadcasting_002c-singleton_002c-Numpy">broadcasting, singleton, Numpy</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Slicing">Slicing</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-built_002din-array">built-in array</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Compatibility">Compatibility</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Indices_cp_letter-C">C</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-cast"><code>cast</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-cell">cell</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Rank-polymorphism">Rank polymorphism</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-collapse"><code>collapse</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-compile_002dtime">compile-time</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Containers-and-views">Containers and views</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-concrete"><code>concrete</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-conj"><code>conj</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-container">container</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Using-the-library">Using the library</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-ct">ct</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Containers-and-views">Containers and views</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Indices_cp_letter-D">D</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-diag"><code>diag</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Indices_cp_letter-E">E</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-early"><code>early</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-elision_002c-index">elision, index</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Slicing">Slicing</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-end_002c-Octave_002fMatlab"><code>end</code>, Octave/Matlab</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Slicing">Slicing</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-error">error</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Extension">Extension</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-every"><code>every</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-explode"><code>explode</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Indices_cp_letter-F">F</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-FFTW">FFTW</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Compatibility">Compatibility</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-foreign-type">foreign type</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Compatibility">Compatibility</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-format_005farray"><code>format_array</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-for_005feach"><code>for_each</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-frame">frame</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Rank-polymorphism">Rank polymorphism</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-from"><code>from</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Indices_cp_letter-G">G</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-Guile">Guile</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Compatibility">Compatibility</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Indices_cp_letter-I">I</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-imag_005fpart"><code>imag_part</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-insert"><code>insert</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Slicing">Slicing</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-iter"><code>iter</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Indices_cp_letter-J">J</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-J">J</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Why-C_002b_002b">Why C++</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Indices_cp_letter-L">L</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-len"><code>len</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Slicing">Slicing</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-length">length</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Overview">Overview</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Indices_cp_letter-M">M</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-map"><code>map</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Indices_cp_letter-N">N</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-none"><code>none</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Containers-and-views">Containers and views</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-none-1"><code>none</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Containers-and-views">Containers and views</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-none-2"><code>none</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-noshape"><code>noshape</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Numpy">Numpy</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Rank-extension">Rank extension</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Numpy-1">Numpy</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Rank-extension">Rank extension</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Numpy-2">Numpy</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Compatibility">Compatibility</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Indices_cp_letter-O">O</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-OpenGL">OpenGL</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Compatibility">Compatibility</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-order_002c-column_002dmajor">order, column-major</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Containers-and-views">Containers and views</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-order_002c-row_002dmajor">order, row-major</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Rank-polymorphism">Rank polymorphism</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-other-libraries_002c-interfacing-with">other libraries, interfacing with</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Compatibility">Compatibility</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-overload-set">overload set</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Extension">Extension</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Indices_cp_letter-P">P</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-pack"><code>pack</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-pick"><code>pick</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-ply"><code>ply</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-prod"><code>prod</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-ptr"><code>ptr</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Python">Python</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Compatibility">Compatibility</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Indices_cp_letter-R">R</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-rank">rank</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Overview">Overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-rank-1"><code>rank</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Introspection">Introspection</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-rank_002c-compile_002dtime">rank, compile-time</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Slicing">Slicing</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-rank_002c-runtime">rank, runtime</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Slicing">Slicing</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-real_005fpart"><code>real_part</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-rel_005ferror"><code>rel_error</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-reverse"><code>reverse</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-rt">rt</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Containers-and-views">Containers and views</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-runtime">runtime</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Containers-and-views">Containers and views</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Indices_cp_letter-S">S</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-scalar"><code>scalar</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-shape">shape</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Overview">Overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-shape-1"><code>shape</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Introspection">Introspection</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-shape-agreement_002c-prefix">shape agreement, prefix</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Rank-extension">Rank extension</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-shape-agreement_002c-suffix">shape agreement, suffix</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Rank-extension">Rank extension</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-size"><code>size</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-sqr"><code>sqr</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-sqrm"><code>sqrm</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-start"><code>start</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Compatibility">Compatibility</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-start-1"><code>start</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-stencil"><code>stencil</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-step">step</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Rank-polymorphism">Rank polymorphism</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-stride">stride</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Rank-polymorphism">Rank polymorphism</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-sum"><code>sum</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-swap"><code>swap</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Indices_cp_letter-T">T</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-TensorIndex">TensorIndex</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Slicing">Slicing</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-transpose"><code>transpose</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Indices_cp_letter-U">U</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-uninitialized-container">uninitialized container</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Containers-and-views">Containers and views</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-uninitialized-container-1">uninitialized container</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Containers-and-views">Containers and views</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Indices_cp_letter-V">V</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-view">view</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Containers-and-views">Containers and views</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-view_002c-rank-0">view, rank 0</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Slicing">Slicing</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Indices_cp_letter-W">W</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-where"><code>where</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-withshape"><code>withshape</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-wrank"><code>wrank</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Indices_cp_letter-X">X</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-xI"><code>xI</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
</table>
<table><tr><th valign="top">Jump to: &nbsp; </th><td><a class="summary-letter" href="#Indices_cp_letter-A"><b>A</b></a>
 &nbsp; 
<a class="summary-letter" href="#Indices_cp_letter-B"><b>B</b></a>
 &nbsp; 
<a class="summary-letter" href="#Indices_cp_letter-C"><b>C</b></a>
 &nbsp; 
<a class="summary-letter" href="#Indices_cp_letter-D"><b>D</b></a>
 &nbsp; 
<a class="summary-letter" href="#Indices_cp_letter-E"><b>E</b></a>
 &nbsp; 
<a class="summary-letter" href="#Indices_cp_letter-F"><b>F</b></a>
 &nbsp; 
<a class="summary-letter" href="#Indices_cp_letter-G"><b>G</b></a>
 &nbsp; 
<a class="summary-letter" href="#Indices_cp_letter-I"><b>I</b></a>
 &nbsp; 
<a class="summary-letter" href="#Indices_cp_letter-J"><b>J</b></a>
 &nbsp; 
<a class="summary-letter" href="#Indices_cp_letter-L"><b>L</b></a>
 &nbsp; 
<a class="summary-letter" href="#Indices_cp_letter-M"><b>M</b></a>
 &nbsp; 
<a class="summary-letter" href="#Indices_cp_letter-N"><b>N</b></a>
 &nbsp; 
<a class="summary-letter" href="#Indices_cp_letter-O"><b>O</b></a>
 &nbsp; 
<a class="summary-letter" href="#Indices_cp_letter-P"><b>P</b></a>
 &nbsp; 
<a class="summary-letter" href="#Indices_cp_letter-R"><b>R</b></a>
 &nbsp; 
<a class="summary-letter" href="#Indices_cp_letter-S"><b>S</b></a>
 &nbsp; 
<a class="summary-letter" href="#Indices_cp_letter-T"><b>T</b></a>
 &nbsp; 
<a class="summary-letter" href="#Indices_cp_letter-U"><b>U</b></a>
 &nbsp; 
<a class="summary-letter" href="#Indices_cp_letter-V"><b>V</b></a>
 &nbsp; 
<a class="summary-letter" href="#Indices_cp_letter-W"><b>W</b></a>
 &nbsp; 
<a class="summary-letter" href="#Indices_cp_letter-X"><b>X</b></a>
 &nbsp; 
</td></tr></table>


<hr>
</div>
<div class="unnumbered" id="Notes">
<div class="header">
<p>
Previous: <a href="#Indices" accesskey="p" rel="prev">Indices</a>, Up: <a href="#Top" accesskey="u" rel="up"><code>ra::</code></a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Notes-1"></span><h2 class="unnumbered">Notes</h2>

<ol>
<li> <code>ra::</code> uses the non standard <code>#pragma once</code> (supported on all major compilers).
</li></ol>

</div>
</div>
<div class="footnote">
<hr>
<h4 class="footnotes-heading">Footnotes</h4>

<h5><a id="FOOT1" href="#DOCF1">(1)</a></h5>
<p>/ə&rsquo;ɹ-eɪ/, I guess.</p>
<h5><a id="FOOT2" href="#DOCF2">(2)</a></h5>
<p>Sometimes &lsquo;strides&rsquo;. Cf. <a href="https://en.wikipedia.org/wiki/Dope_vector"><em>dope vector</em></a></p>
<h5><a id="FOOT3" href="#DOCF3">(3)</a></h5>
<p>Examples given without context assume that one has declared <code>using std::cout;</code>, etc.</p>
<h5><a id="FOOT4" href="#DOCF4">(4)</a></h5>
<p>The brace-list constructors of rank 2 and higher aren&rsquo;t supported on types of rt rank, because in the C++ grammar, a nested initializer list doesn&rsquo;t always define a rank unambiguously.</p>
<h5><a id="FOOT5" href="#DOCF5">(5)</a></h5>
<p>You can still use pointers or <code>std::initializer_list</code>s for shape by wrapping them in the functions <code>ptr</code> or <code>vector</code>, respectively.</p>
<h5><a id="FOOT6" href="#DOCF6">(6)</a></h5>
<p>The brace-list constructors aren&rsquo;t rank extending, because giving the ravel is incompatible with rank extension. They are shape-strict —you must give every element.</p>
<h5><a id="FOOT7" href="#DOCF7">(7)</a></h5>
<p>Prefix agreement is chosen for <code>ra::</code> because of the availability of a <a href="#The-rank-conjunction">rank conjunction</a> [<a href="#Sources">Ber87</a>]
 and <a href="#Cell-iteration">cell iterators of arbitrary rank</a>. This allows rank extension to be performed at multiple axes of an array expression.</p>
<h5><a id="FOOT8" href="#DOCF8">(8)</a></h5>
<p>The multi-argument square bracket form <code>A[i₀, i₁, ...]</code> is supported under C++23 compilers (e.g. gcc ≥ 12 with <code>-std=c++2b</code>), with the same meaning as <code>A(i₀, i₁, ...)</code>. Under C++20 only a single-argument square bracket form <code>A[i₀]</code> is available.</p>
<h5><a id="FOOT9" href="#DOCF9">(9)</a></h5>
<p><code>&amp;&amp;</code>, <code>||</code> are short-circuiting as array operations; the elements of the second operand won&rsquo;t be evaluated if the elements of the first one evaluate to <code>false</code> or <code>true</code>, respectively.
Note that if both operands are of rank 0 and at least one of them is an <code>ra::</code> object, they is no way to preserve the behavior of <code>&amp;&amp;</code> and <code>||</code> with built in types and avoid evaluating both, since the overloaded operators <a href="http://en.cppreference.com/w/cpp/language/operators">are normal functions</a>.</p>
<h5><a id="FOOT10" href="#DOCF10">(10)</a></h5>
<p>Simplified; see the references in <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p1170r0.html">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p1170r0.html</a>.</p>
<h5><a id="FOOT11" href="#DOCF11">(11)</a></h5>
<p>Diagram generated using Graphviz and <a href="https://www.flourish.org/cinclude2dot">https://www.flourish.org/cinclude2dot</a>.
</p>
<pre class="verbatim">cd ra &amp;&amp; cinclude2dot.pl --include . &gt; headers.dot
dot -Tpng headers.dot -Gdpi=100 &gt; headers.png
</pre>
</div>


<a href='js_licenses.html' rel='jslicense'><small>JavaScript license information</small></a>
</body>
</html>
